Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.
Hi,
I am looking for a solution to calculate occurrence dynamically for the past 60 days of the maximum date on the slicer.
The sample dataset is as below:
Customer ID | Visit Date |
A | 6/1/2022 |
A | 6/5/2022 |
A | 5/3/2022 |
B | 2/1/2022 |
B | 4/5/2022 |
B | 5/5/2022 |
For example, when the date slicer set 6/30/2022 as the end date, I would like to calculate the occurrence of each customer in the time range from 5/1/2022- 6/30/2022. The preferred output will be:
Max Date on Slicer | 6/30/2022 |
Customer ID | Visit Counts |
1 | 3 |
2 | 1 |
Similarly, when the end date on the slicer is changed into 5/31/2022, the time range of calculation will switch to 4/1/2022-5-31/2022. The preferred result now will be:
Max Date on Slicer | 5/31/2022 |
Customer ID | Visit Counts |
1 | 2 |
2 | 2 |
Thank you for your assistance in advance!!
Solved! Go to Solution.
Hi @jts_ ,
Here's my solution.
1.Create a calendar table.
Calendar = CALENDAR(DATE(2022,1,1),DATE(2022,12,31))
2.Create a slicer for the max date.
3.Create another measure for counting the visits.
Visit Counts =
CALCULATE (
COUNT ( 'Table'[Customer ID] ),
FILTER (
ALLSELECTED ( 'Table' ),
[Customer ID] = MAX ( 'Table'[Customer ID] )
&& [Visit Date]
>= SELECTEDVALUE ( 'Calendar'[MaxDate] ) - 60
&& [Visit Date] <= SELECTEDVALUE ( 'Calendar'[MaxDate] )
)
)
4.Create a table visual.
When the max date is 6/30/2022,
When the max date is 5/31/2022,
Best Regards,
Stephen Tao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Hi @jts_ ,
Here's my solution.
1.Create a calendar table.
Calendar = CALENDAR(DATE(2022,1,1),DATE(2022,12,31))
2.Create a slicer for the max date.
3.Create another measure for counting the visits.
Visit Counts =
CALCULATE (
COUNT ( 'Table'[Customer ID] ),
FILTER (
ALLSELECTED ( 'Table' ),
[Customer ID] = MAX ( 'Table'[Customer ID] )
&& [Visit Date]
>= SELECTEDVALUE ( 'Calendar'[MaxDate] ) - 60
&& [Visit Date] <= SELECTEDVALUE ( 'Calendar'[MaxDate] )
)
)
4.Create a table visual.
When the max date is 6/30/2022,
When the max date is 5/31/2022,
Best Regards,
Stephen Tao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Hi,
Create a Calendar Table and a relationship (Many to One and Single) from the Visit Date column of the Data Table to the Date column of the Calendar Table. To your slicer visual, drag the Date from the Calendar Table. To your Table visual, drag the CustomerID. Write this measure
Measure 1 = calculate(countrows(Data),datesbetween(calendar[date],max(calendar[date])-59,max(calendar[date])))
Hope this helps.
Hi
I would create a date table from the visit date. Use the below code create a date table. Build the relationship Date.Date to Visit date in your other table.
let
StartDate = List.Min(Table[Visit Date]),
EndDate = Date.EndOfMonth(List.Max(Table[Visit Date])),
Source = List.Dates(StartDate,Duration.Days(EndDate - StartDate),#duration(1,0,0,0)),
#"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
#"Renamed Columns" = Table.RenameColumns(#"Converted to Table",{{"Column1", "Date"}}),
#"Changed Type" = Table.TransformColumnTypes(#"Renamed Columns",{{"Date", type date}}),
#"Inserted Month Name" = Table.AddColumn(#"Changed Type", "Month Name", each Date.MonthName([Date]), type text),
#"Inserted Month" = Table.AddColumn(#"Inserted Month Name", "Month", each Date.Month([Date]), Int64.Type),
#"Inserted Year" = Table.AddColumn(#"Inserted Month", "Year", each Date.Year([Date]), Int64.Type),
#"Inserted Day of Week" = Table.AddColumn(#"Inserted Year", "Day of Week", each Date.DayOfWeek([Date]), Int64.Type),
#"Added Custom" = Table.AddColumn(#"Inserted Day of Week", "YearMonth", each Number.ToText([Year])&Text.Start([Month Name],3)),
#"Changed Type2" = Table.TransformColumnTypes(#"Added Custom",{{"YearMonth", type text}}),
#"Added Custom Column" = Table.AddColumn(#"Changed Type2", "YearMonthSort", each Text.Combine({Date.ToText([Date], "yyyy"), Date.ToText([Date], "MM")}), type text),
#"Inserted Week of Year" = Table.AddColumn(#"Added Custom Column", "Week of Year", each Date.WeekOfYear([Date]), Int64.Type),
#"Inserted Quarter" = Table.AddColumn(#"Inserted Week of Year", "Quarter", each Date.QuarterOfYear([Date]), Int64.Type)
in
#"Inserted Quarter"
Then build a measure as below.
CustVisits =
var eom = eomonth(Max('Date'[Date]),0)
var eom1 = eoMONTH(eom,-2)+1
VAR VISIT= CALCULATE(COUNTROWS('Table'),
FILTER ( ('Table'),
'Table'[Visit Date]>eom-60 && 'Table'[Visit Date]<=EOM))
return VISIT
The date selected is 30/05/2022. i.e may. Table displays only April and May.
Raw data displaying 7 rows (unfiltered)
This works for all months as some months may have 28 days, 30, 31 days.
Let me know if this works for you.
Give this a try.
Hi @grantsamborn ,
Thank you for your response!
I follwed along your suggestion from creating another "DateSlicer" table that does not have any relationship with other tables.
However, the DAX retrieve the data as I wanted when I calculate all occurence that are on or before the selected date on filter ('Visits'[Visit Date]<=MAX(DateSlicer[Date]) ).
But then, when adding the last part to specify the 60 days window ( &&'Visits'[Visit Date]>=MAX(DateSlicer[Date]) -60 ).
All the result turns into blank. Can you please advise the possible reason of it? Thank you so much!
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.
User | Count |
---|---|
110 | |
95 | |
76 | |
65 | |
51 |
User | Count |
---|---|
146 | |
109 | |
106 | |
88 | |
61 |