DAX measure to count IDs satisfying a threshold condition - powerbi

SalePersonId Month Qty
1 Jan-18 5
2 Jan-18 7
1 Feb-18 1
2 Feb-18 8
3 Feb-18 12
I need to create a measure which gives me a count of salespersons whose total sales quantity is more than 10 for the year 2018.
The result should be 2 (Sale person 1 & 3)
I can achieve this in T-SQL with the following query:
SELECT COUNT(Distinct EmpId) FROM T1 GROUP BY UserId HAVING SUM(Qty) > 10
How can I do the same in DAX?

Here's one possible approach:
= COUNTROWS(
FILTER(
SUMMARIZECOLUMNS(
T1[SalePersonId],
"Total", SUM(T1[Qty])),
[Total] > 10))
The SUMMARIZECOLUMNS part is essentially
SELECT SalePersonId, SUM(Qty) AS 'Total' FROM T1 GROUP BY SalePersonId
The FILTER part is equivalent to the HAVING clause and then you just count the rows in the resulting table.

Related

DAX measure calculation based on the different values in column

I would like to create measure which will calculate sum of sales orders.
I have a table:
Sales Order ID
Sales Order Item
Type
Amount
1
01
1
250
1
02
2
300
2
01
1
100
3
01
2
50
If one Sales Order has 1 and 2 type then should be calculated only type 2, when Sales Order has only one type then all value should be calculated. So in that case:
Sales Order 1 = 300
Sales Order 2 = 100
Sales Order 3 = 50
Could you please help me how can I achieve this results?
Best regards
Probably this is what you need:
SumConditional =
var __typ2 = ADDCOLUMNS( SUMMARIZE('Sheet1 (2)', 'Sheet1 (2)'[Sales Order ID]), "MaxType", CALCULATE(MAX('Sheet1 (2)'[Type]) ))
return
CALCULATE( SUM('Sheet1 (2)'[Amount]), TREATAS( __typ2,'Sheet1 (2)'[Sales Order ID],'Sheet1 (2)'[Type]) )

What is the purpose of using VALUES or ALL in the first parameter of an iterator function?

I know that only CALCULATE can modify the filter context. However following are 2 example using VALUES and ALL.
Example 1:
Revenue =
SUMX(
Sales,
Sales[Order Quantity] * Sales[Unit Price]
)
Revenue Avg Order =
AVERAGEX(
VALUES('Sales Order'[Sales Order]),
[Revenue]
)
What is the purpose of VALUES in AVERAGEX function? Is this to add an additional filter context?
Example 2:
Product Quantity Rank =
RANKX(
ALL('Product'[Product]),
[Quantity]
)
What is the purpose of using ALL in an iterator function?
Suppose we have a table like this:
ID
Sales Order
Order Quantity
UnitID
Unit Price
1
101
10
4
39.99
2
101
15
3
24.99
3
102
5
2
15.99
4
103
5
1
14.99
5
103
10
3
24.99
Since the Sales Order column has duplicates,
Revenue Avg Order = AVERAGEX ( VALUES ( Sales[Sales Order] ), [Revenue] )
gives a different result than
Revenue Avg ID = AVERAGEX ( Sales, [Revenue] )
since the first averages over the three Sales Order values whereas the second averages over the five ID rows.
Using DISTINCT instead of VALUES would work too.
Using ALL is instead of VALUES gives the same total but ignores the local filter context from the table visual:
Revenue Avg All = AVERAGEX ( ALL ( Sales[Sales Order] ), [Revenue] )
In this context, ALL is acting as a table function that returns all of the distinct values of the column specified ignoring filter context.

Sum distinct values for first occurance in Power BI with Filter

In Power BI I have some duplicate entries in my data that only have 1 column that is different, this is a "details" column.
Basically, when I sum up my Value column on a Power BI card, I want it to filter IsActive = 1 and sum for each unique name, so in this case:
Total= 10 + 7
Is there any way I can filter this with a DAX formula?
Assuming your table can also have a row with the same value of another row but a different name, and a row where Details column doesn't always include "Feature 1"
Name Values Details IsActive
Item 1 10 Feature 1 1
Item 1 10 Feature 2 1
Item 2 15 Feature 1 0
Item 3 7 Feature 1 1
Item 3 7 Feature 2 1
Item 3 7 Feature 3 1
Item 4 10 Feature 1 1
Item 5 10 Feature A 1
then we should use the Name column an write something like follows
Total =
CALCULATE(
SUMX( SUMMARIZE( T, T[Name], T[Values] ), T[Values] ),
T[IsActive] = 1
)
You can create a calculated column wherein you rank the rows based on the occurrence via M-query as provided in the below link :
https://community.powerbi.com/t5/Desktop/How-to-add-Row-number-over-partition-by-Customer-DAX-or-M-query/td-p/566949
Once the calculated column is done, you can achieve your result based on the below measure :
sum(value) where IsActive=1 and calculatedColumn=1 via on Filter DAX
It doesn't appear that the first occurrence is relevant, so you can just write a measure to sum distinct values.
SUMX (
CALCULATETABLE (
VALUES ( Table1[Value] ),
Table1[IsActive] = 1
),
Table1[Value]
)

Calculate monthly value between 2 tables without an explicit relationship in Power BI model

I am trying to create a measure that calculates (a/qty)*100 for each month,
where qty commes from Delivery table (generated with an R script)
month qty
<date> <dbl>
1 2019-02-01 1
2 2019-03-01 162
3 2019-04-01 2142
4 2019-05-01 719
And a comes from a table TABLE_A that has been created within Power BI that looks like this :
Client Date a
x 2019-03-07 3
x 2019-04-14 7
y 2019-03-12 2
So far, I managed to calculate that value overall with the following measure formula :
MEASURE = CALCULATE( (Sum(TABLE_A[a])/sum(Delivery[qty]))*100)
The issue I have is that I would need this measure monthly (i.e. join the tables on month) without explicitly defining a link between the tables in the PowerBI model.
For each row in TABLE_A you need to look up the corresponding qty in Delivery, so try something along these lines:
MEASURE =
DIVIDE(
SUM( TABLE_A[a] ),
SUMX(
TABLE_A,
LOOKUPVALUE(
Delivery[qty],
Delivery[month], EOMONTH( TABLE_A[Date], -1 ) + 1
)
)
) * 100
The formula EOMONTH( TABLE_A[Date], -1 ) returns the end of the previous month relative to that date and adding 1 day to that to get the start of the current month for that date.

How can I create a total that is filtered by selection and slicer?

Problem
I would like to create a multi-layer histogram that shows the distribution of var1 on the first level and var2 on the second level, with a legend by source, like this:
The value should show the percentage w.r.t. the total of a source, with all the selections and slicers applied. The percentages shown in the histogram should always sum to 100% per source.
Example data
I have the following example data:
source var1 var2 count
A 1 1 100
A 1 2 12
A 1 3 34
A 2 1 1612
A 2 2 23
A 2 3 43
B 1 1 200
B 1 2 320
B 1 3 12
B 2 1 1757
B 2 2 345
B 2 3 32
What have I tried
I can achieve a total per source with the following measure without the filtering part:
percPerSource =
DIVIDE(
SUM(input[count]);
CALCULATE(
SUM(input[count]);
ALLEXCEPT(input;input[source])
)
)*100
If I turn on Drill mode and click on the columns of var1 I get the following, undesired result (the percentages do not sum to 100%):
Another attempt was using the ALLSELECTED function:
percSelected =
DIVIDE(
SUM(input[count]);
CALCULATE(
SUM(input[count]);
ALLSELECTED(input[var1])
)
)*100
This shows only 100% on the var2 level:
I think this will do what you're after:
percPerSource =
DIVIDE(
SUM(input[count]);
CALCULATE(
SUM(input[count]);
FILTER(
ALLSELECTED(input);
input[Source] IN VALUES(input[Source])
)
)
)*100
This takes all the selected values as the universe you are filtering on but only selects the rows that in your local filter context.
The FILTER function takes a table as its first argument and a condition as the second argument. It iterates through every row in the table passed into it and checks if the condition holds and returns a table with only the rows where the condition evaluates to True.
The VALUES function returns a list of distinct values of the column specified evaluated within the local filter context.