DAX Calculate the YOY score change - powerbi

I need to calculate the difference between each year and plot that change in a line graph

In Power BI your pivoted (Excel) table will get you nowhere. Be sure to unpivot all your "YYYY Score" columns in PowerQuery first. This should give you something like:
Next create an explicit measure for your score:
Max Score = MAX('Table'[Score])
With this you can now create your desired "Score Change":
Score Change =
VAR cursor =
MAX ( 'Table'[Year] )
VAR prevYear =
CALCULATE (
MAX ( 'Table'[Year] ),
'Table'[Year] < cursor
)
VAR prevScore =
CALCULATE (
[Max Score],
'Table'[Year] = prevYear
)
RETURN
IF ( NOT ( ISBLANK ( prevScore ) ),
[Max Score] - prevScore
)
Finally put the results in a matrix visual or a line chart:

Related

Show cumulative SalesAmount column chart to max date in slicer

I am looking to build a column chart in Power BI that displays cumulative sales by month up to max date selected in date slicer. I have a supplementary Date2 table that is related to Date table (inactive relation). Tried something like this, but I keep getting all the months irrespective which one is selected in slicer:
Sales to date selected =
VAR ReferenceDate = MAX ( 'Date'[Date] )
Return
CALCULATE ( [SalesAmount],
ALLEXCEPT('Date', 'Date'[Year]),
FILTER( all(Date2[Date]), Date2[Date] <= MAX( Date2[Date] )
&& Date2[Date] <= ReferenceDate),
USERELATIONSHIP ( 'Date'[Date], Date2[Date] )
)
This is what I get as a result (Slicer selects month 7):
It looks like CALCULATE does not honor filter Date2[Date] <= ReferenceDate . What am I doing wrong here?
OK, looks like this scenario solves my issue. I removed inactive relationship between Date and Date2 and introduced active relationship from Sales to Date2.
Sales to date selected =
VAR ReferenceDate = MAX ( 'Date'[Date] )
Return
IF( MAX(Date2[Date]) <= ReferenceDate,
CALCULATE ( [SalesAmount],
ALLEXCEPT('Date', 'Date'[Year]),
Date2[Date] <= MAX( Date2[Date] )
)
)
I made a correction to your code; but It seems that It takes a long time for it to be approved by community members:
So test this:
Sales to date selected =
VAR ReferenceDate =
MAX ( ALLSELECTED ( 'Date2'[Date] ) )
RETURN
CALCULATE (
[SalesAmount],
FILTER ( ALLEXCEPT ( 'Date2', 'Date2'[Year] ), 'Date2'[Date] <= ReferenceDate )
)
Or This:
Sales to date selected =
VAR ReferenceDate =
MAX ( ALLSELECTED ( 'Date2'[Date] ) )
RETURN
CALCULATE (
[SalesAmount],
FILTER ( ALL ( 'Date2'[Date] ), 'Date2'[Date] <= ReferenceDate )
)

Using DAX to Create a dynamic horizontal line for a bar or line chart in Power BI

Hello Lovely People of SO,
I hope you guys are having a great day!
I have the following dataset
REGION
ID
STATUS
DC
1
NEW
ED
2
NEW
FR
3
OLD
FR
4
NEW
GI
5
OLD
GI
6
OLD
GY
7
NEW
GY
8
OLD
GY
9
OLD
GY
10
OLD
GY
11
OLD
GY
12
NEW
RT
13
NEW
TX
14
NEW
TX
15
NEW
I will first want to know how to use DAX to calculate the percentage of ID with STATUS ="OLD" by REGION
Intuitivatly in Python I can group by REGION and then summarize the number of OLD and NEW STATUS but here in PBI DAX things are not somewhat stright forward yet for me since I am learning so thanks you for helping my out with that, my main goal is to create a bar chart that will show the percentage of OLD STATUS by REGION and add a horizontal line that will display the global average of percentages, this is my own data summary
and my desired viz would look like this:
but I have no clue was to how to do that in dax in power bi thank you so much if you guys can help me out If you can reference some code online to do this or if there is a special bulti-in function to calculate this horizontal line thanks a million I will be super attentive to all of your comments
and let me know if It works for you:
OldPercent_Measure =
VAR TblSummary =
ADDCOLUMNS (
SUMMARIZE ( YourTable, YourTable[REGION] ),
"STATUS OLD",
CALCULATE ( COUNTROWS ( YourTable ) + 0, YourTable[STATUS] = "OLD" ),
"TOTAL", CALCULATE ( COUNTROWS ( YourTable ) ),
"% OLD",
ROUND (
DIVIDE (
CALCULATE ( COUNTROWS ( YourTable ) + 0, YourTable[STATUS] = "OLD" ),
CALCULATE ( COUNTROWS ( YourTable ) )
),
2
)
)
VAR PercentOld =
SUMX ( TblSummary, [% OLD] )
VAR GlobalAverage =
AVERAGEX ( TblSummary, [% OLD] )
RETURN
PercentOld
Then create a column chart, put [REGION] column in X_axis, and put OldPercent_Measure in the Y_axis[Values field]. I hope It solves your problem.
To calculate Global Average, the same code above. The only difference is to replace PercentOld with GlobalAverage after "RETURN" statement. Like This:
......
......
RETURN
GlobalAverage
Extra Info:
You want to see the result of your summary table:
EVALUATE
VAR TblSummary =
ADDCOLUMNS (
SUMMARIZE ( YourTable, YourTable[REGION] ),
"STATUS OLD",
CALCULATE ( COUNTROWS ( YourTable ) + 0, YourTable[STATUS] = "OLD" ),
"TOTAL", CALCULATE ( COUNTROWS ( YourTable ) ),
"% OLD",
ROUND (
DIVIDE (
CALCULATE ( COUNTROWS ( YourTable ) + 0, YourTable[STATUS] = "OLD" ),
CALCULATE ( COUNTROWS ( YourTable ) )
),
2
)
)
VAR PercentOld =
SUMX ( TblSummary, [% OLD] )
RETURN
TblSummary
Resulting Screen:
To find out how to put average line:
You need to go analytics pane.
Here is a good link to do that:
https://learn.microsoft.com/en-us/power-bi/transform-model/desktop-analytics-pane
One way to get the average for OLD is to create a calculated column for %. Then use DAX to find the average of just the ones with OLD status, using:
CALCULATE(AVERAGE([% column]), FILTER(table name, STATUS = “OLD”))
The graph is a cummulative histogramm (the second left to right, top line). The line is a constant line at analytics. I used the same measure for value there. Have a nice day.
Rate =
VAR Regions = VALUES(tbl[REGION])
VAR sumOfRates=
SUMX(
Regions
,DIVIDE(
CALCULATE(COUNTROWS('tbl'),tbl[STATUS]="OLD")
,CALCULATE(COUNTROWS('tbl'))
)
)
RETURN
DIVIDE(sumOfRates,COUNTROWS(Regions))+0

Measure Including Year Filter

i have done a Measure to get the Revenue from TOP N Products.
N depends on the selected Value from my TOP-N Table (It's containing just the numbers from 1 to 10).
In the same Measure, I also calculate how much was the revenue of all Products how were not in the selected Top N.
The Code for this is:
Top N Produktbez Sum DB3 =
VAR TopNSelected =
SELECTEDVALUE ( 'TOP N-Filter'[TOP N] )
VAR TopNProdbezTable =
TOPN ( TopNSelected, ALLSELECTED ( 'Pseudo Produkbez Table' ), [DB3] )
VAR TopNProdbez =
CALCULATE ( [DB3], KEEPFILTERS ( TopNProdbezTable ) )
VAR OtherSales =
CALCULATE ( [DB3], ALLSELECTED ( 'Pseudo Produkbez Table' ) )
- CALCULATE ( [DB3], TopNProdbezTable )
VAR CurrentProd =
SELECTEDVALUE ( 'Pseudo Produkbez Table'[Produktbezeichnung] )
RETURN
IF ( CurrentProd <> "Others", TopNProdbez, OtherSales )
Now I want to add Year as Filter.
I'm using the Year coming from the Date Hierarchy.
Unfortunately, I can't provide a sample yet.
If a sample is need, I will try to create own.

I will like a script in power bi that will calculate percentage increase or decrease from one month to the previous month

I want to display percentage increase or decrease in total for each month as I select each month i.e when I click on FEB, it should tell me whether there was a percentage increase/decrease in expenses compared to JAN.
I have tried different codes but keep getting an error message.
Here is a DAX CODE I tried:
change perc =
VAR ValueLastMONTH =
CALCULATE (
SUM ( population[TOTAL] ),
FILTER (
population,
population[MONTH]
= ( EARLIER ( population[MONTH] ) - 1 )
&& population[CATEGORY] = EARLIER ( population[CATEGORY] )
)
)
RETURN
IF (
ISBLANK ( ValueLastMONTH ),
0,
( population[TOTAL] - ValueLastMONTH )
/ ValueLastMONTH
I want a new column created to display the percentage increase or decrease from a month to its previous month.
Here is a screenshot of the excel document:
The Column 'Month' is not of type date. How would PowerBi know the text APR represents April? You need to make this column a date.
Now you need to change the script to work with DateDiff:
change perc =
VAR ValueLastMONTH =
CALCULATE (
SUM ( population[TOTAL] ),
FILTER (
population,
DATEDIFF(population[MONTH], EARLIER ( population[MONTH] ),MONTH) = 1
&& population[CATEGORY] = EARLIER ( population[CATEGORY] )
)
)
RETURN
IF (
ISBLANK ( ValueLastMONTH );
0;
( population[TOTAL] - ValueLastMONTH )
/ ValueLastMONTH)

Pareto (80/20) Segmentation in Power BI

I created 80/20 segmentation in my Power BI data model and I got what I wanted (see the table below).
Now I want to calculate new Name column with the next logic: if Cumulative % <=80% show value from the "Customer Name" column, otherwise show "Other" (the result will be the column Name as in the table below).
I tried with this calculated column but it doesn't work (the result isn't correct, it's always "Other"):
80/20 Name = IF([Cumulative Percen.] <= 0.8, SalesReport[Names], "Other")
Note: "Cumulative Percen." is calculated measure.
How can I do this?
In the next step, I'll use a pie chart to show this segmentation where all customers with small cumulative transactions will be categorized as Other.
The calculated measures that I used:
Customer Rank by Transaction =
IF (
HASONEVALUE ( SalesReport[CustName] ),
RANKX (
ALLSELECTED ( SalesReport[CustName] ),
CALCULATE ( [# of Transactions] ),
,
DESC,
DENSE
)
)
Customer Cumulative Transaction =
VAR CurrentCustimerRank = [Customer Rank by Transaction]
RETURN
SUMX (
FILTER (
ALLSELECTED ( SalesReport[CustName] ),
CALCULATE ( [Customer Rank by Transaction] ) <= CurrentCustimerRank
),
CALCULATE ( DISTINCTCOUNT ( SalesReport[CustID] ) )
)
Customer Cumulative Transaction Percen. =
[Customer Cumulative Transaction]
/ CALCULATE (
DISTINCTCOUNT ( SalesReport[CustID] ),
ALLSELECTED ( SalesReport[CustName] )
)