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)
Related
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.
I want to achive do cumulate values per day per product and reset the value for every new year.
What I have:
Date
productID
value
01.01.2022
1270
30000
01.01.2022
1280
200000
02.01.2022
1280
-50
01.02.2022
1280
100
01.02.2022
1280
200
01.02.2022
1270
-20
01.03.2022
1270
80
29.12.2022
1270
100
29.12.2022
1280
10
31.12.2022
1270
35
31.12.2022
1270
5
01.01.2023
1270
50000
01.01.2023
1280
100000
04.01.2023
1270
50
06.01.2023
1280
-100
Value should be calculated cumulative per day with a fresh start from each year and per productID.
What I want as a measure is Cumulative Per Year.
Date
productID
value
Cumulative per Year
01.01.2022
1270
30000
30000
01.01.2022
1280
200000
200000
02.01.2022
1280
-50
199950
01.02.2022
1280
100
200050
01.02.2022
1280
200
200250
01.02.2022
1270
-20
29980
01.03.2022
1270
80
30060
29.12.2022
1270
100
30160
29.12.2022
1280
10
200260
31.12.2022
1270
35
30195
31.12.2022
1270
5
30200
01.01.2023
1270
50000
50000
01.01.2023
1280
100000
100000
04.01.2023
1270
50
50050
06.01.2023
1280
-100
99900
What I tried:
Cumulative per Year =
VAR varProductID = SELECTEDVALUE(MyTable[productID])
VAR varYear = SELECTEDVALUE(MyTable[date])
CALCULATE(SUM(MyTable[Value],
FILTER(MyTable,
varProductID = MyTable[productID] &&
varYear = MyTable[date]
)
What I also tried is STARTOFYEAR() and ENDOFYEAR() to know when the cumulative should reset but I not meant to work with selectedvalue() also for some reason MyTable[date].Year wont work.
Thanks for any help.
this should work...
Cumulative per Year =
VAR _date =
SELECTEDVALUE ( 'Table'[Date] )
VAR _id =
SELECTEDVALUE ( 'Table'[productID] )
RETURN
CALCULATE (
SUM ( 'Table'[value] ),
FILTER (
ALL ( 'Table' ),
_id = 'Table'[productID]
&& 'Table'[Date].[Year] = YEAR ( _date )
&& 'Table'[Date] <= _date
)
)
What you could try is TOTALYTD time intelligence measure and drop it in as a last column and see if it works.
It would look something like this:
Cummulative Total=TOTALYTD(SUM(Table[Value]),DateTable[Date])
If you don't have it already you will need to create a date dimension table, see link here for a steps on how to create a Calendar table > https://radacad.com/all-in-one-script-to-create-calendar-table-or-date-dimension-using-dax-in-power-bi
Create a relationship between calendar table date > your table date and then use cummulative total measure. Always best to have date dimension table.
My test tabe in powerbi:
IdRecord
Date
Value
1
2022-04-25 23:45:00.000
100
1
2022-04-24 18:07:00.000
344
2
2022-05-01 23:45:00.000
5
2
2022-05-02 18:07:00.000
66
2
2022-05-03 18:07:00.000
31
I require to create a calculated column to mark the earliest of the records grouped by id.
Desired output
IdRecord
Date
Value
IsFirst
1
2022-04-25 23:45:00.000
100
0
1
2022-04-24 18:07:00.000
344
1
2
2022-05-01 23:45:00.000
5
1
2
2022-05-02 18:07:00.000
66
0
2
2022-05-03 18:07:00.000
31
0
Answering to myself
FirstRes= VAR MYMIN = CALCULATE(
MIN(Table[Date]),
FILTER ( Table, Table[IdRecord] = EARLIER(Table[IdRecord]))
)
RETURN
IF(CALCULATE(
MIN(MIN(Table[Date]),MYMIN),
FILTER ( Table, Table[IdRecord] = EARLIER ( Table[IdRecord] ) )
) = Table[Date],1,0)
I'm calculating the difference of "closed column". All data is in one column and I'm calculating the difference between Row2-Row1 for all the rows. I'm getting results as some positive values and some negative. Positive values are coming correct but negative values are incorrect. I'm applying the formula
diff =
Table3[Value] -
CALCULATE(
SUM (Table3[Value]),
FILTER(
Table3,
Table3[Index] = EARLIER(Table3[Index])- 1
)
).
Screenshot of my formula
Output after applying formula, -ve and +ve values
Please help how can I correct my -ve values?
Month Week Month End Closed Open GT IN
01/2020 W01-2020 N 71 178 249 71
01/2020 W02-2020 N 284 189 473 213
01/2020 W03-2020 N 550 210 760 266
01/2020 W04-2020 N 861 185 1046 311
01/2020 W05-2020 Y 1185 205 1390 324
02/2020 W06-2020 N 370 206 576 370
02/2020 W07-2020 N 665 209 874 295
In Power Query Editor, I have added an Index column started from 1 to the data and the output is as below-
Now, create this below measure to get previous rows Closed value in the current row-
prev_row_closed_value =
CALCULATE(
SUM (your_table_name[Closed]),
FILTER(
ALL(your_table_name),
your_table_name[Index] = MIN(your_table_name[Index]) - 1
)
)
For calculating difference, use this below measure-
diff =
MIN(your_table_name[Closed]) -
CALCULATE(
SUM (your_table_name[Closed]),
FILTER(
ALL(your_table_name),
your_table_name[Index] = MIN(your_table_name[Index]) - 1
)
)
Here is output from the above measure-
In Dax you can use the following formulas.
In step one we create a column to get your Week Column in an order:
YearWeek = CONVERT(RIGHT(Sheet1[Week], 4) & MID(Sheet1[Week],2,2),INTEGER)
This is creating an integer value our of your year and month. Next we can use this to get the previous closed amount to be substracted where we filter first on the correct month. Be aware that I take the assumption this is a date column.
In =
var curMonth = Sheet1[Month]
var curYW = Sheet1[YearWeek]
var filterMonthYW = FILTER(Sheet1, curMonth = Sheet1[Month] && curYW > Sheet1[YearWeek])
var MaxYW = CALCULATE(MAX(Sheet1[YearWeek]), filterMonthYW)
return Sheet1[Closed] - CALCULATE(MAX(Sheet1[Closed]), FILTER(filterMonthYW, MaxYW = Sheet1[YearWeek] ))
Sheet1 is your table..
End result:
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.