DAX TOPN Distinct Values - powerbi

Say I have this data structure
O#
Product Type
Sub Revenue
Total Revenue
123
A
$9
$16
123
B
$7
$16
234
C
$3
$12
234
A
$9
$12
557
B
$7
$10
557
C
$3
$10
I would like to do a Top 2 (for example) by Total Revenue that would return the top 2 O# 123 and 234 like this
O#
Product Type
Sub Revenue
Total Revenue
123
A
$9
$16
123
B
$7
$16
234
C
$3
$12
234
A
$9
$12
How do I do it in DAX? I so far have the below but it's not quite working since it returns 123 twice and counts it as twice.
EVALUATE
TOPN (
2,
SUMMARIZECOLUMNS(
[O#],
[Product Type],
[Sub Revenue],
[Total Revenue]
),
[Total Revenue],
DESC
)
Thanks

EVALUATE
FILTER (
ADDCOLUMNS (
SUMMARIZE (
'Table',
'Table'[O#],
'Table'[Product Type],
'Table'[Sub Revenue],
'Table'[Total Revenue]
),
"#ranking", RANKX ( 'Table', 'Table'[Total Revenue],,, DENSE )
),
[#ranking] < 3
)

Related

Sum previous rows in column

I am looking for a solution to sum previous rows in a calculated column.
Sample data:
product | sales | cumulative
000001 | 2000 | 2000
000001 | 2000 | 4000
000002 | 1500 | 1500
000001 | 2000 | 6000
000002 | 1500 | 3000
Could anyone help me with the DAX please.
This is what I would do
step 1)
I would create a new ordinal column with a sequential number for each product group
index =
RANKX (
FILTER (
Sales,
EARLIER ( Sales[Product] ) = Sales[Product]
),
Sales[Sales],
,
ASC
)
Step 2)
I create the Running Totals column that shows the running totals by group
Running Totals =
CALCULATE (
SUM (Sales[Sales]),
FILTER(
Sales,
Sales[Product] = EARLIER ( Sales[Product])
&& Sales[index] <= EARLIER ( Sales[index])
)
)
this is the result

DAX | Sum up rows for a column based on conditions in another column

I am new to DAX, I have a data which looks like -
col1
col2
A
20
A
10
B
30
B
20
My output should be -
col1
col2
col3
A
20
30
A
10
30
B
30
50
B
20
50
I have tried writing a measure but it dosnt work -
col3 =
CALCULATE (
SUM ( Sheet1[col2] ),
FILTER (
ALLSELECTED ( Sheet1 ),
Sheet1[col1] == Sheet1[col1]
)
)
If you are actually looking for a formula to calculate col3, take this one here and put all cols into a table visual.
col3 =
CALCULATE(
SUM('Sheet1'[col2]),
ALLEXCEPT(
'Sheet1',
'Sheet1'[col1]
)
)

DAX formula for sales sum for each day of previous n days

Given a table FACT in Power BI with three columns Date, Category and Sales I am looking for a DAX function that for each day returns the sum of the sales of its previous n days.
Lets assume n = 2, that means for day 01/04/2020 my measure should return the sum of the sales of the days 01/02/2020 and 01/03/2020.
Here is a small example:
Date Category Sales
01/01/2020 A 1
01/01/2020 B 3
01/02/2020 B 2
01/03/2020 B 1
01/04/2020 A 0
01/05/2020 B 10
01/06/2020 B 7
What I want is
Date MyMeasure
01/01/2020 0
01/02/2020 4
01/03/2020 6
01/04/2020 3
01/05/2020 1
01/06/2020 10
I later would then like to use the Category as a filter and in my case n is 365.
I tried the following
MyMeasure =
VAR FROM_DATE =
DATEADD ( FACT[DATE], 0, DAY )
VAR SALES_365 =
CALCULATE (
SUM ( FACT[SALES] ),
DATESINPERIOD ( FACT[DATE], FROM_DATE, 365, DAY )
)
RETURN
SALES_365
but got an error that a table of multiple values was supplied where a single value was expected.
The error was in the variable FROM_DATE, in which DATEADDD returned a column of dates and not a single value.
That said you don't really need a variable for that, you can just use a simple subtraction and DATESBETWEEN(DateCol, DateFrom,DateTo)
MyMeasure =
CALCULATE (
SUM ( FACT[SALES] ),
DATESBETWEEN('FACT'[DATE],MAX(FACT[DATE])-2,MAX(FACT[DATE])-1)
)

How to calculate yearly average per product?

I am new to Power BI and the language DAX. Therefore, I find it hard to do simple yearly average across different products.
I have tried a different combination of the functions AVERAGEX and CALCULATE without any luck so far.
My table:
Date | Product | Value
2014-05-06 Cheese 10
2014-08-11 Cheese 12
2015-04-11 Cheese 9
2014-01-22 Milk 4
2014-12-24 Milk 8
The output I try to create:
Date | Product | Value | Yearly_AVG
2014-05-06 Cheese 10 11
2014-08-11 Cheese 12 11
2015-04-11 Cheese 9 9
2014-01-22 Milk 4 6
2014-12-24 Milk 8 6
Lastly, I do not have a calendar table in the dataset.
For a measure, you can write something like this:
YearAvg =
VAR CurrProduct = VALUES ( Sales[Product] )
VAR CurrYear = YEAR ( MAX ( Sales[Date] ) )
RETURN
CALCULATE (
AVERAGE ( Sales[Value] ),
ALLSELECTED ( Sales ),
Sales[Product] IN CurrProduct,
YEAR ( Sales[Date] ) = CurrYear
)

How to do a cumulative with a duplicate value

[http://uupload.ir/view/v2t_capture.png] We have 2 tables with 2 columns:
table location ->column ->city ,province
table sales column ->actual sales
table location : table sales
province city id sales
tehran eslamshar 1 100
tehran rey 2 500
hamedan tefresh 3 500
esfahan esahan 4 400
gilan rasht 5 400
gilan rar 6 900
I want to calculate 80% of total sales in each city and each province
it means if we have 2800 total sale in whole province, we want just show province that cut off 80 % of sales:
I use this measure but it has one problem that duplicate value does not count in a cumulative sum.
If I have two same sale like 900 it does not count in cumulative and it ignores it.
Cumulative Total =
IF (
NOT ( ISBLANK ( [sales] ) ),
CALCULATE (
[sales],
FILTER (
ALL ( DimLocation[Province] ),
CALCULATE ( [sales], VALUES ( DimLocation[Province] ) ) <= [sales]
)
)
)
Expected Result:
sales cumulative
tehran 800 800
shiraz 200 1000
ghom 200 400
markazi 300 500
Output:
tehran 800 800
shiraz 200 1000
ghom 200 1000
markazi 300 500
For a running total you could create this measure:
RT Province Sales :=
VAR _current = SELECTEDVALUE ( DimLocation[Province] )
RETURN
CALCULATE (
[Sales],
DimLocation[Province] <= _current ,
ALL ( DimLocation[Province] )
)