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
Drobinson1
Helper III
Helper III

previous year filter

Trying to calculate previous year results. based on filter selected from facts table months and years.

 

I am able to get the correct result by hardcoding the previous year such as 2015 but once I try ot use a measure it fails to calculate correctly.

 

CALCULATE(SUM(GL_ACCOUNT_BALANCES1[Value]),FILTER(ALL(GL_ACCOUNT_BALANCES1[ACCOUNT_IDENT]),GL_ACCOUNT_BALANCES1[ACCOUNT_IDENT]="Actual"),FILTER(ALL(GL_ACCOUNT_BALANCES1[CURRENT_YEAR]),GL_ACCOUNT_BALANCES1[CURRENT_YEAR]=2015)))

 

Works with the exception thtat the result is always for 2015 regardless of which year I select in the filter.

 

I would like to select a year and get the previous years results, essentially year-1.

 

 

I have tryed:

 

CALCULATE(SUM(GL_ACCOUNT_BALANCES1[Value]),FILTER(ALL(GL_ACCOUNT_BALANCES1[ACCOUNT_IDENT]),GL_ACCOUNT_BALANCES1[ACCOUNT_IDENT]="Actual"),FILTER(ALL(GL_ACCOUNT_BALANCES1[CURRENT_YEAR]),GL_ACCOUNT_BALANCES1[CURRENT_YEAR]=[PY])))

 

 

[PY]=CALCULATE(MAX(GL_ACCOUNT_BALANCES1[CURRENT_YEAR]),ALLSELECTED(GL_ACCOUNT_BALANCES1))-1

Returns the correct value in my pivotable based on year selected. Example if I select 2014 it returns 2013.

 

I want to use this measure in a formula in place of hard coding 2015.

1 ACCEPTED SOLUTION

@Drobinson1

 

According to this document, VAR Function (DAX) is included in SQL Server 2016 Analysis Services (SSAS), Power Pivot in Excel 2016, and Power BI Desktop.

You should be able to use following measure formula with Power Pivot in Excel 2016.

 

PY Balance =
VAR PY =
    CALCULATE (
        MAX ( GL_ACCOUNT_BALANCES1[CURRENT_YEAR] ),
        ALLSELECTED ( GL_ACCOUNT_BALANCES1 )
    )
        - 1
RETURN
    (
        CALCULATE (
            SUM ( GL_ACCOUNT_BALANCES1[Value] ),
            FILTER (
                ALL ( GL_ACCOUNT_BALANCES1[ACCOUNT_IDENT] ),
                GL_ACCOUNT_BALANCES1[ACCOUNT_IDENT] = "Actual"
            ),
            FILTER (
                ALL ( GL_ACCOUNT_BALANCES1[CURRENT_YEAR] ),
                GL_ACCOUNT_BALANCES1[CURRENT_YEAR] = PY
            )
        )
    )

Best Regards,

Herbert

 

View solution in original post

7 REPLIES 7
CahabaData
Memorable Member
Memorable Member

So if I understand; the top statement works - with 2015 manually entered into the statement.

 

The bottom statement is the same but you want to use a variable [PY]

 

Did you declare [PY] as a variable?

 

 

www.CahabaData.com

I would try opening your measure the variable declaration:

 

YourMeasure = VAR

                          [PY]=CALCULATE(MAX(GL_ACCOUNT_BALANCES1[CURRENT_YEAR]),ALLSELECTED(GL_ACCOUNT_BALANCES1))-1

                          RETURN

                          CALCULATE ( ....your 2nd statement.....

 

 

www.CahabaData.com

I am using powerpivot in excel and it does not seem like var works for measures.

@Drobinson1

 

According to this document, VAR Function (DAX) is included in SQL Server 2016 Analysis Services (SSAS), Power Pivot in Excel 2016, and Power BI Desktop.

You should be able to use following measure formula with Power Pivot in Excel 2016.

 

PY Balance =
VAR PY =
    CALCULATE (
        MAX ( GL_ACCOUNT_BALANCES1[CURRENT_YEAR] ),
        ALLSELECTED ( GL_ACCOUNT_BALANCES1 )
    )
        - 1
RETURN
    (
        CALCULATE (
            SUM ( GL_ACCOUNT_BALANCES1[Value] ),
            FILTER (
                ALL ( GL_ACCOUNT_BALANCES1[ACCOUNT_IDENT] ),
                GL_ACCOUNT_BALANCES1[ACCOUNT_IDENT] = "Actual"
            ),
            FILTER (
                ALL ( GL_ACCOUNT_BALANCES1[CURRENT_YEAR] ),
                GL_ACCOUNT_BALANCES1[CURRENT_YEAR] = PY
            )
        )
    )

Best Regards,

Herbert

 

We are on 2013 :(.

Wish I could just upgrade to 2016.

I am using powerpivot in excel.  VAR does not seem to be an option?

Correct,

Didnt seem like I was able to decalre a variable since it was a measure?

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.