Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

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.

Reply
themistoklis
Community Champion
Community Champion

Compare DateTime Periods and not Date Periods

Hello All,

 

I have a report (attached on this message) with two tables.

The one on the left compares current year/quarter/month/week against the same period in the previous year.

 

The table on the right does a YoY, QoQ, MoM, WoW comparison.

 

The formulas work correctly but when I select DayNames and Hours from the slicers the formulas do not work (expecially the ones that look on previous year/quarter/month data).

That's because the functions (e.g. PREVIOUSMONTH, SAMEPERIODLASTYEAR) only work on dates and not on datetimes.

 

I would like to have tha ability to select Monday and Tusday and compare the metrics for these days only (or hours).

 

Any hep will be much appreciated

1 ACCEPTED SOLUTION
MFelix
Super User
Super User

Hi @themistoklis,

 

Since you have the filtering at a weekday level and hour level you need to change the calculation mode:

 

Views Same Period Last Year V2 = 
VAR Total_Per_Month =
    FILTER (
        SUMMARIZE (
            ALLSELECTED ( 'Calendar Date Hour' ),
            'Calendar Date Hour'[Year-Month],
            "@ViewsCurrentValue", [Views Current],
            "@YearMonth+1",
                LEFT ( 'Calendar Date Hour'[Year-Month], 4 ) + 1
                    & RIGHT ( 'Calendar Date Hour'[Year-Month], 3 )
        ),
        [@YearMonth+1] IN VALUES ( 'Calendar Date Hour'[Year-Month] )
    )
RETURN
    SUMX ( Total_Per_Month, [@ViewsCurrentValue] )

 

What I did was to create a sumarizzation table based on your lowest granularity in this case the Year-Month, then I added on the temporary table the next year month. This allows me to have a table with two columns for the same value this year and next year then I only filter the table to give me the total for those lines

 

Result below has you can see V2 is different from V1

MFelix_0-1619800346473.png

 

MFelix_1-1619800346485.png

 

 

 

 

 

 


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



View solution in original post

3 REPLIES 3
MFelix
Super User
Super User

Hi @themistoklis,

 

Since you have the filtering at a weekday level and hour level you need to change the calculation mode:

 

Views Same Period Last Year V2 = 
VAR Total_Per_Month =
    FILTER (
        SUMMARIZE (
            ALLSELECTED ( 'Calendar Date Hour' ),
            'Calendar Date Hour'[Year-Month],
            "@ViewsCurrentValue", [Views Current],
            "@YearMonth+1",
                LEFT ( 'Calendar Date Hour'[Year-Month], 4 ) + 1
                    & RIGHT ( 'Calendar Date Hour'[Year-Month], 3 )
        ),
        [@YearMonth+1] IN VALUES ( 'Calendar Date Hour'[Year-Month] )
    )
RETURN
    SUMX ( Total_Per_Month, [@ViewsCurrentValue] )

 

What I did was to create a sumarizzation table based on your lowest granularity in this case the Year-Month, then I added on the temporary table the next year month. This allows me to have a table with two columns for the same value this year and next year then I only filter the table to give me the total for those lines

 

Result below has you can see V2 is different from V1

MFelix_0-1619800346473.png

 

MFelix_1-1619800346485.png

 

 

 

 

 

 


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



@MFelix 

 

It works great! Thanks a lot

MFelix
Super User
Super User

Hi @themistoklis ,

 

You need to change the way you get the calculation done in this case filtering the values you need for the hour and days.

 

Add a column for the previous year date:

 

Same Day Last Year = DATEADD('Calendar Date Hour'[Date],-1,year)

 

 

now redo your measure to:

 

Views Same Period Last Year = 
CALCULATE (
    [Views Current],
    FILTER (
        ALL ( 'Calendar Date Hour' ),
        CONTAINS (
            VALUES ( 'Calendar Date Hour'[Same Day Last Year] ),
            'Calendar Date Hour'[Same Day Last Year], 'Calendar Date Hour'[Date]
        )
            && CONTAINS (
                VALUES ( 'Calendar Date Hour'[Hour] ),
                'Calendar Date Hour'[Hour], 'Calendar Date Hour'[Hour]
            )
    )
)

 

Please check attach file if the calculations are correct when filtering. If there is need to have additional changes to the measure please tell me.

 


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.