DAX: how to select max per date & per month - powerbi

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] ) )
)
)

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.

PowerBI Get cumulative value for each day and reset value each year

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.

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)

Conditional Filtering of table in Power BI

I have certain requirement which I am tryin to implement with help of Power BI.
I have data which looks something like as shown below:
Equipment Date Val
AV001 2/6/19 12:10 AM 24
AV001 2/6/19 12:12 AM 22
AV001 2/6/19 12:20 AM 32
AV001 4/6/19 12:24 AM 28
AV001 4/6/19 12:25 AM 25
AV001 4/6/19 12:28 AM 27
AV002 5/6/19 12:00 AM 25
AV002 5/6/19 12:10 AM 24
AV002 6/6/19 12:12 AM 23
AV003 7/6/19 12:03 AM 29
AV003 7/6/19 12:05 AM 26
AV003 7/6/19 12:06 AM 24
AV003 7/6/19 12:09 AM 22
I want to create a table out of this which will be having Equipment, Date & Time with its value.
Like shown below:
AV001 2/6/19 12:20 AM 32
AV001 4/6/19 12:28 AM 27
AV002 5/6/19 12:10 AM 24
AV002 6/6/19 12:12 AM 23
AV003 7/6/19 12:09 AM 22
The desired table should have Equipment , date with latest time and its corresponding Val.
Kindly let me know any possible way to get the desired table.
The hard part here is that you need to group on date but there isn't a column for that. Here's one possibility though where I use INT to truncate the datetime column to just a date.
FILTER (
Table1,
Table1[Date]
= CALCULATE (
MAX ( Table1[Date] ),
FILTER (
Table1,
Table1[Equipment] = EARLIER ( Table1[Equipment] ) &&
INT ( Table1[Date] ) = INT ( EARLIER ( Table1[Date] ) )
)
)
)
If you added INT ( Table1[Date] ) as a calculated column to Table1, then it's a bit simpler:
FILTER (
Table1,
Table1[Date]
= CALCULATE (
MAX ( Table1[Date] ),
ALLEXCEPT ( Table1, Table1[Equipment], Table1[DateInt] )
)
)

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.