In Tableau, how to calculate weighted average on a daily basis? - statistics

My data looks like below:
unit price quantity salesperson date
$10 5 A 1/1
$10 6 B 1/1
$30 9 A 1/1
$30 10 B 1/1
$10 3 A 1/2
$10 5 B 1/2
$20 7 A 1/2
$20 8 B 1/2
In Tableau, how to calculate the average daily unit sales price each salesperson made? Suppose unit price and quantity are measures, salesperson and date are dimensions.
Thanks a lot!

I'll go ahead and give you the LOD expression, so that you'll have something that will work regardless of your table layout.
{ FIXED [salesperson], [date] : SUM([unit price] * [quantity]) / SUM([quantity]) }
That will give you a table that looks like this:
+------------+----------+-------------+------+-----------------+
| unit price | quantity | salesperson | date | Avg Daily Sales |
+------------+----------+-------------+------+-----------------+
| 10 | 5 | A | 1/1 | 22.86 |
| 10 | 6 | B | 1/1 | 22.5 |
| 30 | 9 | A | 1/1 | 22.86 |
| 30 | 10 | B | 1/1 | 22.5 |
| 10 | 3 | A | 1/2 | 17 |
| 10 | 5 | B | 1/2 | 16.15 |
| 20 | 7 | A | 1/2 | 17 |
| 20 | 8 | B | 1/2 | 16.15 |
+------------+----------+-------------+------+-----------------+
That follows the formula you gave in a comment on Nicarus's answer.
Here's a quick calculation, just to confirm that it works.
On 1/2, Salesman A sold:
( (10 * 3) + (20 * 7) ) / (3 + 7)
= (30 + 140) / 10
= 170 / 10
= 17

You want to determine the total amount (in dollars) sold on average, per day.
You can create a calculated field, like so:
SUM([Quantity] * [Unit Price]) / COUNTD([Date])
Then display the data by salesperson:

I think I figured it out:
sum(unit price*quantity)/sum(quantity)

Related

DAX - Cumulative total without date-formatted source data

I am trying to implement a YtD measure for my report in Excel with Power Pivot. My source looks roughly like this:
Table 1
| Month | Store | Branch | Article | Value |
|----------|-------|--------|---------|-------|
| January | 1 | A | Sales | 200 |
| January | 1 | A | Costs | 100 |
| January | 1 | A | Rent | 10 |
| February | 1 | A | Costs | 20 |
| February | 1 | A | Sales | 80 |
| March | 1 | A | Costs | 30 |
| March | 1 | A | Sales | 80 |
| February | 2 | B | Sales | 100 |
| February | 2 | B | Costs | 40 |
| February | 2 | B | Rent | 20 |
Linked to it, are a table Table 2 of months (name - number from 1 to 12), a table Table 3 of unique articles and a table Table 4 of unique stores with their branches.
I want to be able to display YtD for every article depending on the chosen month.
I have measures:
Val. := sum(table1[Value])
YtD1:= calculate(Val., all('Table 2'[Name]))
The former sums across all the values, which are filtered by article in my pivot report. The latter calculates a YtD across all months. It works, but I have to rewrite it so that it responds to filtering the last month and sums from the first month to the selected month.
I have tried to format month numbers to process them as dates (e.g. first day of the month), but couldn't appropriately handle the FORMAT function.
I have also tried to do a sum of months, i.e.:
YtD2= calculate(Val., filter(Table2;Table2[Number]<=2))
which, I hoped, would count months from January to February. It doesn't seem to do any good, resulting in numbers I cannot explain.
My desired output should look like this:
| Store | Sales | | Costs | |
|-------|-------|-----|-------|-----|
| | Val. | YtD | Val. | YtD |
| 1 | 80 | 280 | 20 | 120 |
| 2 | 100 | 100 | 40 | 40 |
if data is filtered by February.
Or
| Store | Sales | | Costs | |
|-------|-------|-----|-------|-----|
| | Val. | YtD | Val. | YtD |
| 1 | 160 | 360 | 50 | 150 |
| 2 | 100 | 100 | 40 | 40 |
if February and March are selected (Val. is displayed for February and March, but YtD from January to March).
Is there a way to implement this in DAX? Can this be done without conversion from month names (or numbers) to some date&
If not, can I get it to work for a month filter instead of a month slicer? That is, if only one month can be selected.
I cannot use variables and similar Power BI features.
Try:
YTD :=
VAR MaxSelectedMonth =
MAX( Table2[Number] )
RETURN
CALCULATE(
[Val.],
FILTER(
ALL( Table2 ),
Table2[Number] <= MaxSelectedMonth
)
)

Dax measure for monthly running total on weekly granularity

I have to visualize an accumulated monthly budget on a daily level.
Means a running total for the individual month on a daily basis.
The budget targets are provided in a table like this:
+------+------------+--------+
| Area | Year-Week | Budget |
+------+------------+--------+
| A | 2020-01 | 50 |
| A | 2020-02 | 25 |
| A | 2020-03 | 50 |
| A | 2020-04 | 25 |
| B | 2020-01 | 50 |
| B | 2020-02 | 50 |
| B | 2020-03 | 50 |
| B | 2020-04 | 75 |
+------+------------+--------+
As you can see, the monthly budget for January is the sum of 2020-01 - 2020-04
The structure is provided in the calendar table. So I have the information on which week belongs to which month.
What I need to do is to create a dax measure that divides the weekly budget by the working days.
THe calendar has also the information of calendar days. For this purpose I have add this measure:
Working Days =
VAR vSelectedMonth =
SELECTEDVALUE ( 'Calendar'[Fiscal Month Number])
RETURN
CALCULATE (
COUNTROWS ( 'Calendar' ),
FILTER (
'Calendar',
'Calendar'[Fiscal Month Number] = vSelectedMonth
&& 'Calendar'[IsWorkingDay] = 1
)
)
Now, how can I divide the weekly budget by the working days for the corresponding week and than to accumulate this information for the whole month?
Based on the provided information the result should be something like that:
+------+------------+----------------+
| Area | Date | Running Budget |
+------+------------+----------------+
| A | 01.01.2020 | 10 |
| A | 02.01.2020 | 20 |
| A | 03.01.2020 | 30 |
| A | 04.01.2020 | 40 |
| A | 05.01.2020 | 50 |
| A | 06.01.2020 | 50 |
| A | 07.01.2020 | 50 |
| A | 08.01.2020 | 55 |
+------+------------+----------------+
Of course, if the corresponding date is not a working day, that value from the day before should be shown.
I am grateful for every help!
Please let me know if you need further information.
Best Regards

How to determine the order in summarize and calculate values in power pivot

i am very new to power pivot and there is this one thing I haven't been able to understand fully. I have this table consisting of Week, value 1 and Value 2.
I want to first summarize all the values for week 1,2,3 and so forth and then divide the sum of value 1 with the sum of value 2. However, when i do a measure power pivot first divide value 1 with value 2 on each row and then summarize them.
This probably is a very basic question but if someone could shed some light on this for me I would be more than happy.
It is not clear what the resulting table you would to see is and this is important to understand in order to determine the correct DAX for a measure.
However given the following input data in table "tablename"
| Week | Value 1 | Value 2 |
| 2018 w1 | 200 | 4 |
| 2018 w2 | 300 | 5 |
| 2018 w3 | 250 | 3 |
| 2018 w4 | 100 | 4 |
The most obvious measure would be
Value1 by Value2 =
divide
( calculate(sum('tablename'[Value 1]))
, calculate(sum('tablename'[Value 2]))
)
This would mean that if you brought this into a table with Week in the context then you would get the following
| Week | Value 1 | Value 2 | Value1 by Value2 |
| 2018 w1 | 200 | 4 | 50 |
| 2018 w2 | 300 | 5 | 60 |
| 2018 w3 | 250 | 3 | 83.33 |
| 2018 w4 | 100 | 4 | 25 |
or if you used this for all weeks your table would be
| Value1 by Value2 |
| 53.125 |

Excel Pivot Chart - % Of Two Values

Let's say I have the following table:
CORP | COAST | CITY | DONE | MISSING | TOTAL
-------------------------------------
New | West | LAX | 2 | 4 | 6
Old | West | SFO | 3 | 3 | 6
New | East | NYC | 4 | 2 | 6
I make a pivot table that looks like this:
CORP:
COAST: NEW | OLD
-------------------------------------
EAST |
SUM OF TOTAL | 6 |
SUM OF DONE | 4 |
SUM OF MISSING | 2 |
WEST
SUM OF TOTAL | 6 | 6
SUM OF DONE | 2 | 3
SUM OF MISSING | 4 | 3
I want to now add a ROW that is the percentage DONE of the TOTAL amount. I.e. something like this:
CORP:
COAST: NEW | OLD
-------------------------------------
EAST |
SUM OF TOTAL | 6 |
SUM OF DONE | 4 |
SUM OF MISSING | 2 |
% DONE | %67|
WEST
SUM OF TOTAL | 6 | 6
SUM OF DONE | 2 | 3
SUM OF MISSING | 4 | 3
% DONE | %33| %50
The formula for % DONE is DONE / TOTAL
I've tried adding another DONE field to the pivot table (i.e. SUM OF DONE2) then setting the value to be % OF, the base field TOTAL and the base value (previous) however that makes % DONE return the value of #N/A. I've tried various combinations of the above however all are returning the same #N/A. I.e. my pivot table is looking like this:
CORP:
COAST: NEW | OLD
-------------------------------------
EAST |
SUM OF TOTAL | 6 |
SUM OF DONE | 4 |
SUM OF MISSING | 2 |
% DONE |#N/A|
WEST
SUM OF TOTAL | 6 | 6
SUM OF DONE | 2 | 3
SUM OF MISSING | 4 | 3
% DONE |#N/A| #N/A
I've tried Googling how to do it but still to no avail. Please let me know if what I am looking for is possible.
On the Analyze tab, add a Calculated field with the formula
= 'DONE'/'TOTAL'
There is an option in Pivot Table Options that allows you to show blank if there is an error, so you don't get an N/A for old/east. You can go into field settings and format the field as a percentage.

Excel formula-based lookup for multiple criteria across multiple sheets?

I have a series of tabs formatted uniformly that contain data that I want to consolidate onto one master data sheet. I'd like to do this without indexing/looking up each sheet individually.
I've created a sample to illustrate the challenge I'm having in searching for data across multiple tabs to create a master data dump. Given the 4 worksheets shown below, the Summary tab has pre-filled values and I'm trying to fill in for the values marked with ??? if possible.
John:
| Month 1 | Month 2 | Month 3
---------------------------------------
Sales | 500 | 1000 | 800
Expenses | 200 | 300 | 250
Total | 300 | 700 | 550
Joe:
| Month 1 | Month 2 | Month 3
---------------------------------------
Sales | 400 | 800 | 600
Expenses | 300 | 280 | 225
Total | 100 | 520 | 375
Mary:
| Month 1 | Month 2 | Month 3
---------------------------------------
Sales | 750 | 850 | 900
Expenses | 275 | 325 | 400
Total | 475 | 525 | 500
Summary:
Agent | Period | Sales | Expanses | Total
-------------------------------------------
Joe | Month 1 | ??? | ??? | ???
Joe | Month 2 | ??? | ??? | ???
Joe | Month 3 | ??? | ??? | ???
John | Month 1 | ??? | ??? | ???
John | Month 2 | ??? | ??? | ???
John | Month 3 | ??? | ??? | ???
Mary | Month 1 | ??? | ??? | ???
Mary | Month 2 | ??? | ??? | ???
Mary | Month 3 | ??? | ??? | ???
In your sample, the detail sheet data fits exactly in the summary, except that it is transposed. If this is the case for the actual data as well, you may be best off if you copy each chunk of detail data (3x3 in sample) and then paste special - transpose in succession (in case it helps transpose is a check box which appears on the paste special dialog).
I like Steve Klein's answer for simplicity and ease of understanding. Here's an alternative if you really need it to be dynamic. "Sales", "Expenses", "Month1", "Month2", etc. as well as "Joe", "John" & "Mary", need to be exactly as in the detail sheets and the detail sheet names.
=INDEX(INDIRECT($B2&"!"&"$A$1:$D$6"),MATCH(D$1,INDIRECT($B2&"!a:a"),0),MATCH($C2,INDIRECT($B2&"!3:3"),0))

Resources