Power Bi measure: Find average total spend for each ID - powerbi

I want to create a measure in Power Bi that finds the average of the total_spend for each ID. My table looks like this:
ID
Total_Spend
1234
£34.00
1234
£34.00
1234
£ 34.00
4325
£ 56.00
4325
£ 56.00
4325
£ 43.00
4325
£ 43.00
5678
£ 12.00
5678
£ 12.00
I've tried:
AvgIDSpend=
AVERAGEX (
SUMMARIZE (
Table,
Table[ID],
"Total Average", SUM( DISTINCT ( Table[Total_spend] )
),
[Total Average]
)
But got this error- The SUM function only accepts a column reference as an argument.

The quickest way to do it would be to just create a measure that calculate the average of Table[Total_spend], and then use it in a table visual alongside Table[ID].
First, the measure :
Avg_Total_spend =
CALCULATE(
AVERAGE(Table[Total_spend])
)
Then, you create a new table visual with columns:
Table[ID]
Table[Avg_Total_spend]
Or, as a table expression :
ADDCOLUMNS(
SUMMARIZE(
Table,
Table[ID]
),
"Average Table_spend",
Table[Average_Table_spend]
)

Related

power bi dax, sum up all latest monthly entries

Hi I have a data table in powerbi structured
id date data
1 2022-10-30 123
1 2022-11-01 130
1 2022-11-30 456
the data spans multiple user ids and multiple years and it the values are cumulative (like minutes on a phone plan for instance). This is not the real data
I want to add up the end of month data. In the ideal case, my table would be complete and 2022-10-31 would exist for instance, then I could do
Measure =
CALCULATE(
SUM( 'Table'[data] ),
'Table'[dates] = EOMONTH( 'Table'[dates],0 )
)
This returns 456 but I want 579 (123+456). So i cannot use EOMONTH
I think the answer is some combination of the dax above and
FILTER( Table, Table[date] = MAX( Table[date] ) )
Though if I paste that in solo, it grabs the actual latest date only, not all monthly latest dates
Also I will use slicers on user ID's in case that changes the DAX
Please use this measure to get what you need:
Measure_ =
VAR TblSummary =
ADDCOLUMNS ( YoursTable, "EOM", CALCULATE ( ENDOFMONTH ( YoursTable[date] ) ) )
RETURN
SUMX ( TblSummary, IF ( [EOM] = [date], [data] ) )
If we test our above measure on a table visual:

Power BI / DAX show count of distinct values in column

I'd like to expand the solution on this page.
The original solution is given in Power query. But I'd like a DAX solution...
I've created a table from the fields Source and Preparer. If I want to see those sources where there is only one preparer what's is the best way to do it via DAX?
Like the Sources "MN", "RT" and "PO".
Source
Preparer
AB
June
AB
Jane
MN
June
SS
Kim
SS
Lee
SS
Jim
RT
Kim
PO
June
PO
June
RT
Kim
The outcome I'm looking for is like:
Source
Preparer
Number
MN
June
1
RT
Kim
1
PO
June
1
DAX that comes close looks like:
TableResult =
FILTER (
SUMMARIZECOLUMNS (
'Table'[Source],
"Number", DISTINCTCOUNT ( 'Table'[Preparer] )
),
[Number] = 1
)
This gives the source and the number, but not the Preparer....
Any ideas?
Please try this
newMeasure =
CALCULATE (
DISTINCTCOUNT ( 'Table 1'[Preparer] ),
FILTER (
GROUPBY ( 'Table 1', 'Table 1'[Source], 'Table 1'[Preparer] ),
CALCULATE (
DISTINCTCOUNT ( 'Table 1'[Preparer] ),
ALLEXCEPT ( 'Table 1', 'Table 1'[Source] )
) = 1
)
)

PowerBI Rankx Returning only 1

I have a table called Commissions summary that are for sales reps
This table generates a summary row for each sales rep for each month. I want to rank the sales rep only by each month, however my formula is only returning 1's.
The table can be simplified down to this
Sales reps Target Attainment
John, deer 53%
Jane, Joe 160%
Adam, Smith 30%
My goal is to show my table like this
Sales reps Target Attainment Rank
John, deer 53% 2
Jane, Joe 160% 1
Adam, Smith 30% 3
This is the Rankx formula I am using
Ranks =
RANKX (
ALL ( 'Commission Summary' ),
CALCULATE (
SUM ( 'Commission Summary'[Target Attainment] ),
'Commission Summary'[Date] = 10 / 01 / 2019
),
,
DESC
)
As I mentioned earlier, I am only filtering my date to October.
Please help :(
the problem occurs into calculate's filter 'Commission Summary'[Date] = 10 / 01 / 2019. The engine is dividing each number first 10/01 = 10 and 10/2019 = 0.00495 that date doesn't exist in your data returning blank for all values giving as a result a ranking with 1 in all ocurrences (because all are the same blank value).
To avoid this problem use 'Commission Summary'[Date] = DATE(2019,10,1) as filter context. That will return a ranking properly sorted, but you'll have ranked all sales reps including sales reps who had no activity that date (they will be in the bottom of the ranking)
If you want to go a step further i recommend using the following steps:
create a measure first:
Comission Summary Filtered = CALCULATE (
SUM ( 'Commission Summary'[Target Attainment] ),
'Commission Summary'[Date] = DATE(2019,10,1)
Then filter by that measure with the following code:
Rank =
IF (
[Comision Summary Filtered] > 0,
RANKX ( ALL ( 'Commission Summary' ), [Comision Summary Filtered],, DESC, DENSE )
)
With that you'll have a cleaner result. :)

Filtering Before Groupingby / Summarize in Powerbi

I am having some troubles in grouping and filtering in powerbi. I want to be clear about the question so, my goal is to make everything on DAX and not using the filters in the reports. I am doing this because I will create other tables that will relate to this one.
So, let´s say I have one table like this called sales:
Order Customer Product Date Amount Type
A001 John TV 01/02/2019 200,00 ok
A002 Mark Chair 10/02/2019 150,00 ok
A003 John Bed 15/02/2019 50,00 Cancelled
A004 Mark TV 17/02/2019 75,00 ok
A004 Susan Table 19/02/2019 20,00 ok
A005 John Pillow 21/02/2019 50,00 ok
I want to get another table like this:
Customer Orders Last Order First Order Amount Type
John 2 21/02/2019 01/02/2019 250,00 ok
John 1 15/02/2019 15/02/2019 50,00 Cancelled
Mark 2 17/02/2019 10/02/2019 225,00 ok
Susan 1 19/02/2019 19/02/2019 20,00 ok
So, if I did not considered if the product was cancelled or not, the formula to create another table would be:
GROUPBY = (Sales;
'sales'[Customer]
"orders"; countx(currentgroup();'sales'[order]);
"last_order";MAXX(currentgroup();'sales'[Date]);
"first_order";MINX(currentgroup();'sales'[Date]);
"Amount";SUMX(currentgroup();'sales'[Amount]))
The question is, how can I groupby the type and understand if the order was cancelled or not?
tks
Personally, I prefer SUMMARIZECOLUMNS to GROUP BY in simple situations as it's less verbose.
Summary =
SUMMARIZECOLUMNS (
Sales[Customer],
Sales[Type],
"orders", COUNT ( Sales[Order] ),
"last_order", MAX ( Sales[Date] ),
"first_order", MIN ( Sales[Date] ),
"Amount", SUM ( Sales[Amount] )
)
You can simply add "Type" as a grouping field:
Summarized Sales =
GROUPBY (
'sales',
'sales'[Customer],
'sales'[Type],
"orders", COUNTX ( CURRENTGROUP (), 'sales'[order] ),
"last_order", MAXX ( CURRENTGROUP (), 'sales'[Date] ),
"first_order", MINX ( CURRENTGROUP (), 'sales'[Date] ),
"amount", SUMX ( CURRENTGROUP (), 'sales'[Amount] )
)
Result:

Calculate price based on distinct count

I am having trouble working out a measure (Revenue) in power bi.
I have a measure which is basically counting distinct values in a table (table 1). From this column I want to multiply the distinct count to get the total price (prices are in another table).
See below for an example
Table 1
Product DistinctCount Revenue (Measure I am trying to Calculate)
A 15 45.00
B 30 60.00
Prices Table
Product Price
A 3.00
B 2.00
At the moment the Revenue is calculating based on COUNT and not DISTINCTCOUNT.
Any help would be much appreciated.
thanks!
Measures, Calculated Columns, Google
I am assuming you have a relationship set up between these two tables on [Product]. If this is the case you can do something like this to create a calculated column:
Revenue =
CALCULATE (
SUMX ( 'Table 1', 'Table 1'[DistinctCount] * RELATED ( 'Prices Table'[Price] ) )
)
If you are trying to create a table visual try the DAX below, where ID is just a transaction ID for each product in your 'Table 1':
Revenue =
VAR DistinctCountOfProductTransactions =
CALCULATE ( DISTINCTCOUNT ( 'Table'[Id] ) )
VAR Result =
CALCULATE (
DistinctCountOfProductTransactions * SUM ( Prices[Price] ),
TREATAS ( VALUES ( 'Table'[Product] ), Prices[Product] )
)
RETURN
Result