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
Dunner2020
Post Prodigy
Post Prodigy

filter data inside the measure

Hi there,

 

I have about 10 measures that read the data from same table. I apply some inclusion rules on the data before performing any calculation in each measure. The problem is that I have not found an efficient way of applying inclusion rules within the measure. For example, if a measure calculates the difference between two dates (after applying inclusion rules), it looks like as follow:

 

measure 1 =

Var start_date = Calculate(Max(Table[startDate]),Filter(Table,[inclusion1]&&[inclusion2]&&[inclusion3]&&[inclusion4]&&[inclusion5]))

Var end_date = Calculate(Max(Table[endDate]),Filter(Table,[inclusion1]&&[inclusion2]&&[inclusion3]&&[inclusion4]&&[inclusion5]))

Return

Datediff(start_date,end_date,hour)

 

The above example is a simple example, but in some measure, I have 6 to 7 variables and writing same Filter() statement with every variable is cumbersome. Is there any other smart way of filtering the data within measure 

1 ACCEPTED SOLUTION

This is what I was suggesting, although I am not clear about your "inclusion" terms.  Is that shorthand?  What is the actual text for [inclusion1], [inclusion2], ...?

 

NewMeasure =
VAR filtertable =
    FILTER (
        ALL (
            Table[inclusion_col1],
            Table[inclusion_col2],
            Table[inclusion_col3],
            Table[inclusion_col4],
            Table[inclusion_col5]
        ),
        [inclusion1]
            && [inclusion2]
            && [inclusion3]
            && [inclusion4]
            && [inclusion5]
    )
VAR start_date =
    CALCULATE (
        MAX ( Table[startDate] ),
        filtertable
    )
VAR end_date =
    CALCULATE (
        MAX ( Table[endDate] ),
        filtertable
    )
RETURN
    DATEDIFF (
        start_date,
        end_date,
        HOUR
    )

 

 

Regards,

Pat

 

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

3 REPLIES 3
Dunner2020
Post Prodigy
Post Prodigy

@mahoneypat , I used following statement inside the measure to filter the data:

 

Var filter_data = 

FILTER(ALL('Table',Table[inclusion_col1],Table[inclusion_col2],Table[inclusion_col3]),[inclusion1]&&[inclusion2]&&[inclusion_col3])
 
When I tried to extract the column value of a particular column from the filter data variable using the following statement
Var start_date = Max(filter_data[startDate])
it greyed out [startDate].
Could you please help me out?

This is what I was suggesting, although I am not clear about your "inclusion" terms.  Is that shorthand?  What is the actual text for [inclusion1], [inclusion2], ...?

 

NewMeasure =
VAR filtertable =
    FILTER (
        ALL (
            Table[inclusion_col1],
            Table[inclusion_col2],
            Table[inclusion_col3],
            Table[inclusion_col4],
            Table[inclusion_col5]
        ),
        [inclusion1]
            && [inclusion2]
            && [inclusion3]
            && [inclusion4]
            && [inclusion5]
    )
VAR start_date =
    CALCULATE (
        MAX ( Table[startDate] ),
        filtertable
    )
VAR end_date =
    CALCULATE (
        MAX ( Table[endDate] ),
        filtertable
    )
RETURN
    DATEDIFF (
        start_date,
        end_date,
        HOUR
    )

 

 

Regards,

Pat

 

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


mahoneypat
Employee
Employee

You could first store your Filter table in a variable too, and then use it inside each CALCULATE in the other measures.  That way, you are only creating that table once.  Also, you should avoid Filtering the whole "Table".  Instead you should FILTER(ALL(Table[Column1], Table[Column2], ...) including only the columns on which you are evaluating inclusion criteria.

 

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


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.