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
Anonymous
Not applicable

Extract a Single Value from a Column with Duplicates, Filtered by Another Column

Hi,

 

I am wrestling with what I think should be a very simple DAX measure problem, but nothing I have tried yet has worked. Here's the situation:

 

I have a table with colulmns [Academic Year] and [Current Year Flag]. There are duplicate values in both of these columns, since they are part of a table that inludes all classes offered at an institution, and details about those classes. Because multiple classes are offered in a year, there are duplicate year values.

 

Anyway, my goal is to calculate a measure (we'll call it Metric Previous Year) based on a particular column's values from a previous year, i.e., current academic year - 1. Someone before me (using an earlier data warehouse source with different architecture and different field names) approached it like this:

 

Metric Previous Year =

CALCULATE (

    [Metric],

    FILTER (

        ALL ( 'Term Dim' ),

        'Term Dim'[Academic Year Numeric]

            = MAX ( 'Term Dim'[Academic Year Numeric] ) - 1

            && 'Term Dim'[Quarter] IN VALUES ( 'Term Dim'[Quarter] )

    )

)

 

In other words, they used MAX to find the largest value in [Academic Year Numeric] and subtracted 1 to find the previous year. But in the data warehous source I am working with, there are some [Academic Year] values for years in the future, meaning that MAX will return a higher number than the current year, and if I subtract 1 I will not get last year.

What I need to do is extract a single value for [Academic Year] where [Current Year Flag] = True, then use this year to subtract and find the previous year. As a beginner, it seems like I should be able to do this with FILTER() and DISTINCT(), but something confusing about the context or something else I don't understand has prevented me from getting this working. Any suggestions are greatly appreciated!

 

Thanks!

1 ACCEPTED SOLUTION

Looks like I left out a paren or something, try:

 

Metric Prior Year = 
CALCULATE (
    [Metric],
    FILTER (
        ALL ( 'dw B2 Class View' ),
        'dw B2 Class View'[Academic Year]
            = MAXX 
               ( 
                 FILTER(
                   'dw B2 Class View', 
                   [Current Year Flag] = 1 && 
                   'dw B2 Class View'[Academic Year] IN DISTINCT ( 'dw B2 Class View'[Academic Year] )
                 ), //end second FILTER
                 [Academic Year]
              ) //end MAXX
              - 1
    ) // end first FILTER
) // end CALCULATE

 

I also replaced VALUES with DISTINCT. I just wrote a blog article on this topic, but that wasn't because of you. https://community.powerbi.com/t5/Community-Blog/Soapbox-Series-Adding-No-VALUE-S/ba-p/1059818


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

4 REPLIES 4
Greg_Deckler
Super User
Super User

First, sample data would be tremendously helpful: Please see this post regarding How to Get Your Question Answered Quickly: https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490

 

But, perhaps:

CALCULATE (

    [Metric],

    FILTER (

        ALL ( 'Term Dim' ),

        'Term Dim'[Academic Year Numeric]

            = MAXX ( FILTER('Term Dim',[Current Year Flag] = 1,[Academic Year Numeric] ) - 1

            && 'Term Dim'[Quarter] IN VALUES ( 'Term Dim'[Quarter] )

    )

)

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

Hi @Greg_Deckler,

 

You're absolutely right - I should have provided sample data. It would have made it easier for people to answer my question, and it would have helped me catch an important omission I didn't notice in my original question. The metric in question (which I called [Metric]) is actually a calculated measure:

 

Metric =
DIVIDE ( SUM ( [Net Revenue] ), SUM ( [Revenue] ) )

 

 

Using the updated field and table names (from the newer schema I'm working off of, with a table name of 'dw B2 Class View'), your proposed solution would - I think - look like this:

 

Metric Prior Year = 
CALCULATE (
    [Metric],
    FILTER (
        ALL ( 'dw B2 Class View' ),
        'dw B2 Class View'[Academic Year]
            = MAXX ( FILTER('dw B2 Class View', [Current Year Flag] = 1,[Academic Year] ) - 1
            && 'dw B2 Class View'[Academic Year] IN VALUES ( 'dw B2 Class View'[Academic Year] )
    )
)
)

 

 

The MAXX function seems like the right thing to use, but I run into a "too many arguments were passed into the FILTER function" problem. And I think that is actually the root of the problem - trying to figure out how to filter the table based on one column while returning a single related value from a different column.

 

Thank you again so much for your help, and sorry for not getting my question right the first time.

The sample data (from the newer schema I'm working off of, with a table name of 'dw B2 Class View') is as follows:
RevenueNet RevenueAcademic YearCurrent Year Flag
100802015FALSE
2501002015FALSE
40-202015FALSE
5355202016FALSE
5425002016FALSE
886-702017FALSE
5465222017FALSE
3853802018FALSE
518632002018FALSE
55122018FALSE
691550002019FALSE
486-252019FALSE
54202020TRUE
538-1002020TRUE
4873002020TRUE
9975002020TRUE
573722021FALSE
331-202021FALSE
12-802022FALSE
347502022FALSE
7987002022FALSE

 

Looks like I left out a paren or something, try:

 

Metric Prior Year = 
CALCULATE (
    [Metric],
    FILTER (
        ALL ( 'dw B2 Class View' ),
        'dw B2 Class View'[Academic Year]
            = MAXX 
               ( 
                 FILTER(
                   'dw B2 Class View', 
                   [Current Year Flag] = 1 && 
                   'dw B2 Class View'[Academic Year] IN DISTINCT ( 'dw B2 Class View'[Academic Year] )
                 ), //end second FILTER
                 [Academic Year]
              ) //end MAXX
              - 1
    ) // end first FILTER
) // end CALCULATE

 

I also replaced VALUES with DISTINCT. I just wrote a blog article on this topic, but that wasn't because of you. https://community.powerbi.com/t5/Community-Blog/Soapbox-Series-Adding-No-VALUE-S/ba-p/1059818


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

Thank you very much! I appreciate the help.

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.

Top Solution Authors