Forum Discussion

matmat's avatar
matmat
New Member
1 year ago
Solved

Dynamic column header showing incorrect month and Year

Hi

I'm trying to create a matrix visual which shows the Top 5 pages based on visits for current month, previous month and same period last year based on slicer selection for month and Year. I'm using the following dax measures

 

CurrentMonthDate = DATE(
    SELECTEDVALUE('Calendar'[Year]),
    SELECTEDVALUE('Calendar'[Month Num]),1)
 
PreviousMonthDate = EDATE('Calendar'[CurrentMonthDate], -1)
 
SameMonthLastYearDate = EDATE('Calendar'[CurrentMonthDate], -12)
 
I'm using the following  field parameter to show the Month & Year and the values 
Parameter 3 = {
    (FORMAT('Calendar'[CurrentMonthDate], "MMM YYYY"), NAMEOF('GA4_SitePages'[SelectedMonthVisits]), 2),
    (FORMAT('Calendar'[PreviousMonthDate],"MMM YYYY"), NAMEOF('GA4_SitePages'[PreviousMonthVisits]), 1),
    (FORMAT('Calendar'[SameMonthLastYearDate], "MMM YYYY"), NAMEOF('GA4_SitePages'[SamePeriodLastYearVisits]), 0)
}
If I select, Nov 2024 in the slicer, the matrix is showing Dec1898, Nov 1899, Dec 1899 for the Same period last year, previous month and current month. 

 

 

How can I fix this?
Thanks in advance.
  • Hi matmat ,

     

    The issue arises because the CurrentMonthDate, PreviousMonthDate, and SameMonthLastYearDate measures are not properly retrieving the selected values from the slicer, leading to incorrect dates. To fix this, you need to ensure that the CurrentMonthDate measure accurately captures the slicer selection. Use the following DAX formula to calculate the CurrentMonthDate:

    CurrentMonthDate = 
    DATE(
        MAX('Calendar'[Year]), 
        MAX('Calendar'[Month Num]), 
        1
    )
    

    This ensures that the selected Year and Month Num from the slicer are properly reflected. For the PreviousMonthDate and SameMonthLastYearDate, you can use the corrected CurrentMonthDate as the basis for calculating the offsets:

    PreviousMonthDate = 
    EDATE([CurrentMonthDate], -1)
    
    SameMonthLastYearDate = 
    EDATE([CurrentMonthDate], -12)
    

    In your field parameter, replace references to 'Calendar'[CurrentMonthDate] with the measure [CurrentMonthDate], ensuring that the field parameter reflects the updated logic. Here's the corrected Parameter 3:

    Parameter 3 = {
        (FORMAT([CurrentMonthDate], "MMM YYYY"), NAMEOF('GA4_SitePages'[SelectedMonthVisits]), 2),
        (FORMAT([PreviousMonthDate], "MMM YYYY"), NAMEOF('GA4_SitePages'[PreviousMonthVisits]), 1),
        (FORMAT([SameMonthLastYearDate], "MMM YYYY"), NAMEOF('GA4_SitePages'[SamePeriodLastYearVisits]), 0)
    }
    

    With these changes, ensure that your slicer is connected to the Year and Month Num columns in the Calendar table. Verify that your Calendar table contains the correct range of dates and that the slicer selection is being passed correctly. This should resolve the issue and display the correct data for the current month, previous month, and the same period last year in the matrix visual.

     

    Best regards,

  • Hi matmat 
    It seems like the issue might be related to how the dates are being calculated and formatted. Pls check the follwing steps.

    1. Ensure that the CurrentMonthDate measure is correctly calculating the date based on the selected year and month
    2. The EDATE function should work correctly, but let's ensure the CurrentMonthDate is being passed correctly.
    3. Verify the Field Parameter Formatting - The FORMAT function should correctly format the dates, but let's ensure the dates are being calculated correctly before formatting.

     

    Could you please provide sample data to fix your issue. Make sure that, when you are sharing the data don't include sensitive data. Thanks

3 Replies

  • Hi matmat ,

     

    The issue arises because the CurrentMonthDate, PreviousMonthDate, and SameMonthLastYearDate measures are not properly retrieving the selected values from the slicer, leading to incorrect dates. To fix this, you need to ensure that the CurrentMonthDate measure accurately captures the slicer selection. Use the following DAX formula to calculate the CurrentMonthDate:

    CurrentMonthDate = 
    DATE(
        MAX('Calendar'[Year]), 
        MAX('Calendar'[Month Num]), 
        1
    )
    

    This ensures that the selected Year and Month Num from the slicer are properly reflected. For the PreviousMonthDate and SameMonthLastYearDate, you can use the corrected CurrentMonthDate as the basis for calculating the offsets:

    PreviousMonthDate = 
    EDATE([CurrentMonthDate], -1)
    
    SameMonthLastYearDate = 
    EDATE([CurrentMonthDate], -12)
    

    In your field parameter, replace references to 'Calendar'[CurrentMonthDate] with the measure [CurrentMonthDate], ensuring that the field parameter reflects the updated logic. Here's the corrected Parameter 3:

    Parameter 3 = {
        (FORMAT([CurrentMonthDate], "MMM YYYY"), NAMEOF('GA4_SitePages'[SelectedMonthVisits]), 2),
        (FORMAT([PreviousMonthDate], "MMM YYYY"), NAMEOF('GA4_SitePages'[PreviousMonthVisits]), 1),
        (FORMAT([SameMonthLastYearDate], "MMM YYYY"), NAMEOF('GA4_SitePages'[SamePeriodLastYearVisits]), 0)
    }
    

    With these changes, ensure that your slicer is connected to the Year and Month Num columns in the Calendar table. Verify that your Calendar table contains the correct range of dates and that the slicer selection is being passed correctly. This should resolve the issue and display the correct data for the current month, previous month, and the same period last year in the matrix visual.

     

    Best regards,

  • Hi matmat 
    It seems like the issue might be related to how the dates are being calculated and formatted. Pls check the follwing steps.

    1. Ensure that the CurrentMonthDate measure is correctly calculating the date based on the selected year and month
    2. The EDATE function should work correctly, but let's ensure the CurrentMonthDate is being passed correctly.
    3. Verify the Field Parameter Formatting - The FORMAT function should correctly format the dates, but let's ensure the dates are being calculated correctly before formatting.

     

    Could you please provide sample data to fix your issue. Make sure that, when you are sharing the data don't include sensitive data. Thanks

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi matmat ,

     

    Thanks for reaching out.

    Pls has your problem been solved? If so, accept the reply as a solution. This will make it easier for the future people to find the answer quickly.

    If not, please provide a more detailed description, preferably some virtual sample data, and the expected results.

     

    Best Regards,

    Stephen Tao