Excel Calculate Average Based On Value In Another Cell

Excel Average Calculator Based on Cell Values

Calculate conditional averages in Excel with this interactive tool. Enter your data range and criteria to get instant results.

]=?/.test(criteriaValue); const numericValue = parseFloat(criteriaValue.replace(/[<>]=?/, '')); data.forEach(item => { let matches = false; if (isNumericComparison) { const itemValue = parseFloat(item[criteriaProperty]); if (criteriaValue.startsWith('>=')) { matches = itemValue >= numericValue; } else if (criteriaValue.startsWith('>')) { matches = itemValue > numericValue; } else if (criteriaValue.startsWith('<=')) { matches = itemValue <= numericValue; } else if (criteriaValue.startsWith('<')) { matches = itemValue < numericValue; } } else { matches = item[criteriaProperty].toString() === criteriaValue; } if (matches) { matchingValues.push(item.sales); // Always use sales for average in this demo matchingWeights.push(item.weight); count++; } }); return { matchingValues, matchingWeights, count }; } function getColumnData(data, columnLetter) { let property; switch(columnLetter) { case 'A': property = 'id'; break; case 'B': property = 'category'; break; case 'C': property = 'region'; break; case 'D': property = 'sales'; break; case 'E': property = 'rating'; break; default: property = 'sales'; } return data.map(item => item[property]); } function calculateSimpleAverage(values) { if (values.length === 0) return 0; const sum = values.reduce((a, b) => a + b, 0); return sum / values.length; } function calculateWeightedAverage(values, weights) { if (values.length === 0 || weights.length === 0) return 0; let sumProduct = 0; let sumWeights = 0; for (let i = 0; i < values.length; i++) { sumProduct += values[i] * weights[i]; sumWeights += weights[i]; } return sumProduct / sumWeights; } function displayResults(average, count, formula) { const resultsDiv = document.getElementById('wpc-results'); document.getElementById('wpc-average-result').textContent = average.toFixed(2); document.getElementById('wpc-count-result').textContent = count; document.getElementById('wpc-formula-result').textContent = formula; resultsDiv.style.display = 'block'; // Scroll to results resultsDiv.scrollIntoView({ behavior: 'smooth' }); } function updateChart(values, weights) { const ctx = document.getElementById('wpc-chart').getContext('2d'); // Destroy previous chart if it exists if (resultsChart) { resultsChart.destroy(); } // Prepare data const labels = values.map((_, i) => `Item ${i+1}`); const backgroundColors = values.map(() => `rgba(${Math.floor(Math.random() * 255)}, ${Math.floor(Math.random() * 255)}, ${Math.floor(Math.random() * 255)}, 0.7)`); const chartData = { labels: labels, datasets: [{ label: weights ? 'Weighted Values' : 'Values', data: values, backgroundColor: backgroundColors, borderColor: backgroundColors.map(c => c.replace('0.7', '1')), borderWidth: 1 }] }; // Create new chart resultsChart = new Chart(ctx, { type: 'bar', data: chartData, options: { responsive: true, maintainAspectRatio: false, scales: { y: { beginAtZero: true, title: { display: true, text: weights ? 'Weighted Value' : 'Value' } }, x: { title: { display: true, text: 'Data Points' } } }, plugins: { legend: { position: 'top', }, title: { display: true, text: weights ? 'Weighted Value Distribution' : 'Value Distribution', font: { size: 16 } }, tooltip: { callbacks: { label: function(context) { let label = context.dataset.label || ''; if (label) { label += ': '; } if (weights) { label += `$${context.raw.toFixed(2)} (Weight: ${weights[context.dataIndex]})`; } else { label += `$${context.raw.toFixed(2)}`; } return label; } } } } } }); } });

Leave a Reply

Your email address will not be published. Required fields are marked *