How to create chain index USING dax? - powerbi

I have a table with department, category, year and sales. See the sample table below:
Table name: ABC
department
category
year
sales
Finance
1
2012
20
HR
1
2012
30
Marketing
1
2012
60
Finance
2
2012
50
HR
2
2012
15
Marketing
2
2012
17
Finance
1
2013
60
HR
1
2013
40
Marketing
1
2013
90
Finance
2
2013
7
HR
2
2013
20
Marketing
2
2013
22
Finance
1
2014
50
HR
1
2014
39
Marketing
1
2014
120
Using the DAX query language, I was able to create the relative index
department
category
year
sales
relative_index
Finance
1
2012
20
100
HR
1
2012
30
100
Marketing
1
2012
60
100
Finance
2
2012
50
100
HR
2
2012
15
100
Marketing
2
2012
17
100
Finance
1
2013
60
(60/20)*100 = 300
HR
1
2013
40
(40/30)*100 = 133
Marketing
1
2013
90
(90/60)*100 = 150
Finance
2
2013
7
(7/50)*100 = 14
HR
2
2013
20
(20/15)*100 = 133
Marketing
2
2013
22
(22/17)*100 = 129
Finance
1
2014
50
(50/60)*100 = 83
HR
1
2014
39
(39/40)*100 = 97.5
Marketing
1
2014
120
(120/90)*100 = 133
I used the following dax code to create the relatie_index
Relative_Link =
//Inception = get the minimum year
var Inception = MIN(ABC[year])
//FY_LY = get the previous year
var FY_LY = ABC[year]-1
//LY_level = get the previous year's sales for a department and category
previous year
var LY_level = calculate(sum(ABC[sales]), filter(allexcept(ABC, ABC[department],
ABC[category]), ABC[year]=FY_LY))
return if(ABC[year]=Inception, 100, (ABC[sales]/LY_level)*100)
I am having trouble creating the chain_index column
department
category
year
sales
relative_index
chain_index
Finance
1
2012
20
100
100
HR
1
2012
30
100
100
Marketing
1
2012
60
100
100
Finance
2
2012
50
100
100
HR
2
2012
15
100
100
Marketing
2
2012
17
100
100
Finance
1
2013
60
(60/20)*100 = 300
(100*300)/100 = 300
HR
1
2013
40
(40/30)*100 = 133
(100*133)/100 = 133
Marketing
1
2013
90
(90/60)*100 = 150
(100*150)/100 = 150
Finance
2
2013
7
(7/50)*100 = 14
(100*14)/100 = 14
HR
2
2013
20
(20/15)*100 = 133
(100*133)/100 = 133
Marketing
2
2013
22
(22/17)*100 = 129
(100*129)/100 = 129
Finance
1
2014
50
(50/60)*100 = 83
(83*300)/100 = 249
HR
1
2014
39
(39/40)*100 = 97.5
(97.5*133)/100 = 130
Marketing
1
2014
120
(120/90)*100 = 133
(133*150)/100 = 199.5
I am trying to use the following formula:
Chain_Index =
//Inception = get the min year
var Inception = MIN(ABC[year])
//FY_LY = get the previous year
var FY_LY = ABC[year]-1
//Next_Year = Inception + 1
var next_year = Inception + 1
//LY_ChainIndex_Value = get the previous chain index value from previous year
var LY_ChainIndex_Value = calculate(sum(ABC[chain_index]), filter(allexcept(ABC,
ABC[department], ABC[category]), ABC[FY_CY]=FY_LY))
return if(ABC[FY_CY]=Inception, 100, (ABC[relative_index]*LY_ChainIndex_Value)/100)
I am getting the following error message:
A circular dependency was detected: ABC[chain_index].
I am trying to create chain index value described in this youtube video:
https://www.youtube.com/watch?v=TXzNvxCB0_g&t=234
Thanks for reading and help will be appreciated

After some quick testing, the following column works great to calculate the chain_index:
chain_index =
VAR _ly = [year] - 1
VAR _ly_index =
CALCULATE (
SUM ( 'ABC'[relative_index] ) ,
ALLEXCEPT ( 'ABC' , 'ABC'[department] , 'ABC'[category] ) ,
'ABC'[year] = _ly
)
RETURN
IF (
ISBLANK ( _ly_index ) ,
100 ,
( [relative_index] * _ly_index ) / 100
)
Basing the relative_index on the following:
relative_index =
VAR _ly = [year] - 1
VAR _ly_sales =
CALCULATE (
SUM ( 'ABC'[sales] ) ,
ALLEXCEPT ( 'ABC' , 'ABC'[department] , 'ABC'[category] ) ,
'ABC'[year] = _ly
)
RETURN
IF (
ISBLANK ( _ly_sales ) ,
100 ,
100.0 * ( [sales] / _ly_sales )
)
I suspect the circular dependency is caused by whatever it is you are doing to calculate this column 'ABC'[FY_CY] - a column you do not need for this calculation at least.
Unless you are planning to deploy these indexing columns for categorization or slicing, then the best practice is to calculate these values using measures instead of making your data model bigger with calculated columns.

Related

DAX: Cumulative Completion Rate with Month Slicer

I'm trying to calculate cumulative completion rate by all users over moths, the issue is that in the below table for ex when I filter on october it divides users who finished till october / all users except those who finished in November.
I have a dim_date table which is connect to the data table, the retaltion is between Date from dim_date and Completion Date from Data table
Also in dim date table im numbering the months 1,2,3,4 etc
ID
Completion_status
Completion Date
1
0
2
0
3
0
4
0
5
0
6
1
11/1/2022
7
1
11/1/2022
8
1
11/1/2022
9
1
11/2/2022
10
1
11/1/2022
11
1
11/6/2022
12
1
11/4/2022
13
1
11/2/2022
14
1
10/13/2022
15
1
10/14/2022
16
1
10/14/2022
17
1
10/13/2022
18
1
10/15/2022
19
1
10/13/2022
20
1
10/13/2022
21
1
10/13/2022
22
1
10/13/2022
23
1
10/18/2022
24
1
10/13/2022
25
1
10/13/2022
26
1
10/13/2022
27
1
10/13/2022
28
1
9/10/2022
29
1
9/8/2022
the formula I use
Completion% =
VAR comp rate = SUM(Table[completion_status]) / count(Table[ID])
Return
CALCULATE(Table[Completion%],filter(ALL(Dim_Date),Dim_Date[Month Number] <= MAX(Dim_Date[Month Number])))
the expected result when I filter
on september is 2/29 = 7%
on october is 16/29 = 55%
on November is 24/29 = 83%
Something like:
=
VAR SelectedMonth =
MIN( Dim_Date[Month Number] )
VAR CumulativeTotal =
CALCULATE(
COUNTROWS( 'Table' ),
FILTER(
ALL( Dim_Date ),
Dim_Date[Month Number] <= SelectedMonth
&& NOT ( ISBLANK( Dim_Date[Month Number] ) )
)
)
VAR CountAllRows =
CALCULATE( COUNTROWS( 'Table' ), ALL( Dim_Date ) )
RETURN
DIVIDE( CumulativeTotal, CountAllRows )
I'm presuming that Dim_Date[Month Number] is blank when Table[Completion Date] is blank.
You may want to replace ALL with, for example, ALLSELECTED, depending on your required set-up.

DAX - Rankx by multiple Categories Issue

I have 4 Categories (GP, ID, Age, Date). I would would like to create calculated column and group by GP, ID, and Age and Rank/ count by Date to see how many months each member has in past 24 month.
My Code works until I have members who cancelled their membership for a few months and then resumed after. I need to restart from the first month after skip. for example :
GP ID AGE DATE RKING Desired RANK
1 220 35-44 202206 12 6
1 220 35-44 202205 12 5
1 220 35-44 202204 12 4
1 220 35-44 202203 12 3
1 220 35-44 202202 12 2
1 220 35-44 202201 12 1
1 220 35-44 202012 24 24
1 220 35-44 202011 23 23
1 220 35-44 202010 22 22
1 220 35-44 202009 21 21
1 220 35-44 202008 20 20
1 220 35-44 202007 19 19
1 220 35-44 202006 18 18
1 220 35-44 202005 17 17
1 220 35-44 202004 16 16
… … … … … …
1 220 35-44 201901 1 1
This is what I have tried but doesn't work for dates skipping.
RKING Column=
RANKX (
CALCULATETABLE (
VALUES ('tbl'[Date] ),
ALLEXCEPT ( 'tblW', 'tbl'[GP], 'tbl'[ID] ),
'tbl'[AGE] = 'tbl'[AGE],
'tbl'[date] >= start_date && 'tbl'[date] <= end_date // date slicer
),
[Date] ,
,ASC
)
Looking through the code you were trying to make a measure for a visual (For a calcCol the measure is added as well). And as I got a point, you want to show a sum of consequtive months in a matrix for each date in accordance to ID/GP/AGE/DATE I see a following way.
As you know, calculations performs for each row in a matrix and filter the data model according to data presented in matrix rows and columns (slicers as well). So, my idea is -
Get date from matrixRow and use it as max date for the table.
Then use a FILTER(). FILTER() is an iterative function, so it goes throw each row and checks filtering condition - if true row remains if false - not.
I use following filtring conditions:
Get dateInMatrix-dateInACurrentTableRow (for example: 202203-202201= 2 months)
Then check how many rows in the table with min=202201 and max<202203
if there are less rows then date difference then it FALSE() and the row is out of table.
3) The last step is counting of rows it a filtered table.
A measure for matrix:
Ranking =
VAR matrixDate=MAX('table'[DATE])
VAR filteredTable =
FILTER(
ALL('table')
,DATEDIFF(
DATE(LEFT([DATE],4),RIGHT([DATE],2),1)
,DATE(LEFT(matrixDate,4),RIGHT(matrixDate,2),1)
,MONTH
)
=
VAR dateInRow=[DATE]
RETURN
CALCULATE(
COUNTROWS('table')
,'table'[DATE]>=dateInRow
,'table'[DATE]<matrixDate
)
)
RETURN
COUNTROWS(filteredTable)
[![enter image description here][1]][1]
A measure for calcColl:
RankColl =
VAR currentDate=[Start_Date]
Var MyFilt={('Table'[AGE],'Table'[ID],'Table'[GROUP])}
VAR withColl =
ADDCOLUMNS(
CALCULATETABLE(
'table'
,ALL('Table')
,TREATAS(MyFilt,'Table'[AGE],'Table'[ID],'Table'[GROUP])
)
,"dateDiff",
DATEDIFF(
[Start_Date]
,currentDate
,MONTH
)
,"RowsInTable",
VAR dateInRow=[Start_Date]
Var startDate=IF(dateInRow<currentDate,dateInRow,currentDate)
VAR endDay =IF(dateInRow>currentDate,dateInRow,currentDate)
VAR myDates = GENERATESERIES(startDate,endDay,1)
RETURN
COUNTROWS(
CALCULATETABLE(
'Table'
,ALL('Table')
,TREATAS(MyFilt,'Table'[AGE],'Table'[ID],'Table'[GROUP])
,TREATAS(myDates,'Table'[Start_Date])
)
)
)
VAR filtered =
FILTER(
withColl
,[dateDiff]=[RowsInTable]-1 -- for ex.:
-- dateDiff=01/01/2022-01/01/2022=0,
-- but it will be 1 row in the table for 01/01/2022
)
RETURN
CountRows( filtered)

Group by and then sum value

I am struggling to get this going and could need some help. I have the following setup:
Order Item Material Value
22 1 100 27,5
22 1 200 27,5
22 1 300 27,5
22 2 100 33
22 3 500 101
26 1 500 88
26 1 600 88
I have duplicate values becaue of the Material, so I want to group by Order, Item and Value and then calculate the total Value in a DAX measure.
After grouping:
Order Item Value
22 1 27,5
22 2 33
22 3 101
26 1 88
The final Value:
Total Measure = 249,5
I tried the following DAX expression for the Total Measure:
Total Measure = Summarize('Table1'; 'Table1'[Order]; 'Table1'[Item]; "Sum Value:"; Sum('Table1'[Value]))
It gives me the error:
Multiple columns cannot be converted to a scalar value
So I tried:
Total Measure = Sumx('Table1'; Summarize('Table1'; 'Table1'[Order]; 'Table1'[Item]; "Sum Value:"; Sum('Table1'[Value])))
But this didnt work either. For every help thanks in advance.
The following code should be what you are looking for
Measure1 =
SUMX (
SUMMARIZE (
Table1;
Table1[Order];
Table1[Item];
Table1[Value];
"TotalSum"; SUM ( Table1[Value] )
);
[Value]
)
In this case, you can simply use the VALUES function instead of SUMMARIZE.
Total Measure = SUMX ( VALUES ( Table1[Value] ), [Value] )
This iterates over each unique Value and adds Value to the sum.

DAX: how to select max per date & per month

I have a table that keeps Date and Quantity, I need to define MAX Quantity each Month at Date level.
Here is an example:
Date Quantity Max Quantity per Month
01.02.19 20 351 40 952
02.02.19 14 176 40 952
03.02.19 25 218 40 952
23.02.19 13 244 40 952
24.02.19 14 021 40 952
25.02.19 33 173 40 952
26.02.19 21 233 40 952
01.04.19 11 855 40 952
24.04.19 19 113 40 952
25.04.19 40 952 40 952
26.04.19 37 460 40 952
Here MAX Qty in February is 33 173, in April 40 952
But my current measure displays a total max of 40 952
Here is DAX used:
Max Quantity per Month =
CALCULATE(MAXX (
SUMMARIZE (
'Table1',
'Date'[Year Month],
'Table1'[Date],
"Qty", [Quantity]
),
[MAX_Qty]
), ALLEXCEPT('Table1', 'Table1'[Date], 'Date'[Year Month]))
What correct DAX should be to display different value per month?
You can use:
Max Quantity per Month =
CALCULATE (
MAX ( Table1[Quantity] ),
FILTER (
Table1,
MONTH ( Table1[Date] ) = MONTH ( EARLIER ( Table1[Date] ) )
&& YEAR ( Table1[Date] ) = YEAR ( EARLIER ( Table1[Date] ) )
)
)

Power BI What if analysis

I have a matrix Power BI visualization which is like
Jan Feb Mar April
Client1 10 20 30 10
Client2 15 25 65 80
Client3 66 22 54 12
I have created 3 what if parameters slicer table (having values from 1 to 4) for each client
For example, If the value of the first slicer is 1 and the second is 2 and the third is 2 then I want
Jan Feb Mar April
Client1 0 20 30 10
Client2 0 0 65 80
Client3 0 0 54 12
That is, it should replace the value with zero. I have been able to achieve that for one client using Dateadd function (by adding month)
Measure = CALCULATE(SUM('Table'[Value]),
DATEADD('Table'[Column], Parameter[Parameter Value], MONTH))
and I have used this measure to display the value, but how to make it work for the other two clients as well .
Let say you have three parameter tables as follows
Parameter1 Parameter2 Parameter3
Value1 Value2 Value3
------ ------ ------
1 1 1
2 2 2
3 3 3
4 4 4
and each of them has its own slicer. Then the measure you are after might look something like this:
Measure =
VAR Val1 = MAX(Parameter1[Value1])
VAR Val2 = MAX(Parameter2[Value2])
VAR Val3 = MAX(Parameter3[Value3])
VAR CurrClient = MAX('Table'[Client])
VAR CurrMonth = MONTH(DATEVALUE(MAX('Table'[Month]) & " 1, 2000"))
RETURN SWITCH(CurrClient,
"Client1", IF(CurrMonth <= Val1, 0, SUM('Table'[Value])),
"Client2", IF(CurrMonth <= Val2, 0, SUM('Table'[Value])),
"Client3", IF(CurrMonth <= Val3, 0, SUM('Table'[Value])),
SUM('Table'[Value])
)
Basically, you read in each parameter and compare them to the month in the current cell.