Forum Discussion

jts_'s avatar
jts_
Icon for Helper I rankHelper I
4 years ago
Solved

HELP! Calculate occurrence dynamically based on date slicer

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 IDVisit Date
A6/1/2022
A6/5/2022
A5/3/2022
B2/1/2022
B4/5/2022
B5/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 Slicer6/30/2022
Customer IDVisit Counts
13
21

 

 

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 Slicer5/31/2022
Customer IDVisit Counts
12
22

 

 

 

Thank you for your assistance in advance!!

  • Anonymous's avatar
    Anonymous
    4 years ago

    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.

     

     

     

5 Replies

  • Give this a try.

    Visit Count =
    CALCULATE(
    DISTINCTCOUNT(Visits[Visit Date]),
    'Visits'[Visit Date] <= MAX(DateSlicer[Date])
    && 'Visits'[Visit Date] >= MAX(DateSlicer[Date]) - 60
    )
    ('DateSlicer' should be a disconnected table (no relationships) consisting of a list of dates.)
    • jts_'s avatar
      jts_
      Icon for Helper I rankHelper I

      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!

  • 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.

     

  • 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.

  • Anonymous's avatar
    Anonymous
    Not applicable

    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.