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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Anonymous
Not applicable

Calculate date filter

Hello, 

 

I ve been trying to improve my calculation with additional date filter, but it doesnt make any diff. It behaves like it calculates the result for all dates

lukaszEltwin1_0-1671910890079.png

 

Everything is calculates in one table. measure reacts correctly to date slicer or filter visual modification, but only not for dax...

 

Aby idea what can be wrong?

 

1 ACCEPTED SOLUTION

@Anonymous 
Please try

Yield Mech. assemblyprevmonth =
VAR CurrentYearMonth =
    MAX ( 'Date'[Year Month Number] )
VAR MechAssembly =
    CALCULATETABLE (
        TJob,
        TJob[SekundærNavn] = "Mech. assembly",
        'Date'[Year Month Number] = CurrentYearMonth - 1,
        ALL ( 'Date' )
    )
RETURN
    ROUND (
        DIVIDE (
            SUMX ( MechAssembly, TJob[FirstPassQty] ),
            SUMX ( MechAssembly, TJob[Antal] )
        ),
        4
    )

View solution in original post

17 REPLIES 17
Anonymous
Not applicable

Anonymous
Not applicable

Hi @Anonymous ,

 

Please try this measure:

 

(Try PARALLELPERIOD(TJob[Dato],-1,MONTH) instead of PREVIOUSMONTH(TJob[Dato]))

 

PrevMonth = 
CALCULATE(SUMX(Sales, Sales[Unit Cost]), PARALLELPERIOD('Date'[Date],-1,MONTH))

 

 

Then the result is as follows.

vtangjiemsft_0-1672040714565.png

Best Regards,

Neeko Tang

If this post  helps, then please consider Accept it as the solution  to help the other members find it more quickly. 

Anonymous
Not applicable

@Anonymous  Calculated you presented isnt filtering any date. Its result of sumx with function all to each month? Could you try to download contoso file. Maybe my settings for file are disrupting the calculation... I m running out of ideas

 

https://static1.squarespace.com/static/5c3a30e0f7939271ddaa7e4f/t/63a0b153266500608e08b098/1671475547070/ContosoDBGenerator-PowerBIDemo.pbix

lukaszEltwin1_0-1672057756701.png

 

Anonymous
Not applicable

https://drive.google.com/file/d/1Ry9C5zrcUi59CV6j3GqAYjngYlr3wIYg/view?usp=sharing

 

Link for contoso model, same problem with different example

tamerj1
Super User
Super User

Hi @Anonymous 

bring CALCULATE at line number 2 instead of 5

Anonymous
Not applicable

Could you elaborate more? You mean putting sumx as VAR?

 

Yield Mech. assembly =
VAR nom =
    CALCULATE( SUMX(TJob, TJob[Antal]), TJob[SekundærNavn] = "Mech. assembly" , PREVIOUSMONTH(TJob[Dato]))
VAR den =
    CALCULATE(SUMX(TJob, TJob[FirstPassQty]), TJob[SekundærNavn] = "Mech. assembly", PREVIOUSMONTH(TJob[Dato]))
RETURN
    ROUND(
        DIVIDE (
            den,
             nom),
            4)

lukaszEltwin1_0-1671915177660.png

 

@Anonymous 

Please try

Yield Mech. assembly111 =
CALCULATE (
VAR MechAssembly =
FILTER ( TJob, TJob[SekundarNavn] = "Mech. assembly" )
RETURN
ROUND (
DIVIDE (
SUMX ( MechAssembly, TJob[Antal] ),
SUMX ( MechAssembly, TJob[FirstPassQty] )
),
4
),
PREVIOUSMONTH ( TJob[Dato] )
)

Anonymous
Not applicable

Idk know why but the measure keeps displaying blank after previous month function. So as few others i had try for example

 

Date column is correctly formatted as date, rest as whole number.

@Anonymous 
Does this work for you?

1.png

Anonymous
Not applicable

Could you reference measure to VAR version. Contoso model looks very good, refering to year month number is pretty nifty idea, but when i tried to copy results....

 

lukaszEltwin1_0-1672140086353.png

 

It still wont give me the previous month

@Anonymous 
Can you copy/paste this code so I can modify?

Anonymous
Not applicable

Yield Mech. assemblyprevmonth =
VAR MechAssembly =
    FILTER ( TJob, TJob[SekundærNavn] = "Mech. assembly" )
RETURN
    CALCULATE(ROUND(
        DIVIDE (
            SUMX ( MechAssembly, TJob[FirstPassQty] ),
            SUMX ( MechAssembly, TJob[Antal] ) ),
            4),
            ALL('Date'),
            'Date'[Year Month Number] = MAX('Date'[Year Month Number]) -1)

@Anonymous 
Please try

Yield Mech. assemblyprevmonth =
VAR CurrentYearMonth =
    MAX ( 'Date'[Year Month Number] )
VAR MechAssembly =
    CALCULATETABLE (
        TJob,
        TJob[SekundærNavn] = "Mech. assembly",
        'Date'[Year Month Number] = CurrentYearMonth - 1,
        ALL ( 'Date' )
    )
RETURN
    ROUND (
        DIVIDE (
            SUMX ( MechAssembly, TJob[FirstPassQty] ),
            SUMX ( MechAssembly, TJob[Antal] )
        ),
        4
    )
Anonymous
Not applicable

You re a God!

 

The magic question what is the purpose of previousmonth function if its not working... or why is it not working

@Anonymous 
It should work but I usually don't like to use time intellagence functions. You may try

Yield Mech. assemblyprevmonth =
VAR CurrentYearMonth =
    MAX ( 'Date'[Year Month Number] )
VAR MechAssembly =
    CALCULATETABLE (
        TJob,
        TJob[SekundærNavn] = "Mech. assembly",
        PREVIOUSMONTH ( 'Date'[Date] )
    )
RETURN
    ROUND (
        DIVIDE (
            SUMX ( MechAssembly, TJob[FirstPassQty] ),
            SUMX ( MechAssembly, TJob[Antal] )
        ),
        4
    )

@Anonymous 
Forgot to mention that it returns blank if the date table is not filtered i.e. at the grand total level.

Anonymous
Not applicable

And it returns blank. Well it doesnt matter right now. Thank you.

 

To bad dax guides for time intelligence are showing easy results, nothing real life databases. At least for me

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

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