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

In table complex comparison to derive new column

Have the following table data

 

usageDateuserNameproductNameusageHourstokensConsumed
1/1/2020joea54
1/2/2020johnb225
1/3/2020joea24
1/1/2020johnb225
1/2/2020johna224
1/2/2020johnb45

 

I'm trying to add a column with the following criteria, but can't figure out how to do it... 

 

Value is:

- The number of tokens consumed, in the previous day, by the same user id (if exists), in the same product (if exists), if previous day hours > 20 (if exists)

 

So the desired result would be something like this (notes column added to explain value, don't expect this column)

 

usageDateuserNameproductNameusageHourstokensConsumedpreviousTokensnotes
1/1/2020joea540no usage at all by joe on 12/31/19
1/2/2020johnb555john used product b for >20 hrs on 1/1/20
1/3/2020joea240no usage at all by joe on 1/2/20
1/1/2020johnb2250no usage at all by john on 12/31/19
1/2/2020johna2240john did not use product a on 1/1/20
1/3/2020johnb450john used product b on 1/2 but not for more than >20 hrs

 

is this kind of inline comparison possible? or maybe i have to create some temporary tables to use?

1 ACCEPTED SOLUTION
v-yingjl
Community Support
Community Support

Hi @Anonymous ,

You can create a measure to get the expected result:

Previous Token = 
VAR _currentdate =
    SELECTEDVALUE ( 'Table'[usageDate] )
VAR _currentname =
    SELECTEDVALUE ( 'Table'[userName] )
VAR _currentproduct =
    SELECTEDVALUE ( 'Table'[productName] )
VAR _previoushour =
    CALCULATE (
        MAX ( 'Table'[usageHours] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            [usageDate] = _currentdate - 1
                && [userName] = _currentname
                && [productName] = _currentproduct
        )
    )
VAR _previoustoken =
    CALCULATE (
        MAX ( 'Table'[tokensConsumed] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            [usageDate] = _currentdate - 1
                && [userName] = _currentname
                && [productName] = _currentproduct
        )
    )
RETURN
    IF ( _previoushour > 20, _previoustoken, 0 )

result.png

Here is the sample file hopes to help you, please try it: PBIX 

 

Best Regards,
Yingjie Li

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

View solution in original post

1 REPLY 1
v-yingjl
Community Support
Community Support

Hi @Anonymous ,

You can create a measure to get the expected result:

Previous Token = 
VAR _currentdate =
    SELECTEDVALUE ( 'Table'[usageDate] )
VAR _currentname =
    SELECTEDVALUE ( 'Table'[userName] )
VAR _currentproduct =
    SELECTEDVALUE ( 'Table'[productName] )
VAR _previoushour =
    CALCULATE (
        MAX ( 'Table'[usageHours] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            [usageDate] = _currentdate - 1
                && [userName] = _currentname
                && [productName] = _currentproduct
        )
    )
VAR _previoustoken =
    CALCULATE (
        MAX ( 'Table'[tokensConsumed] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            [usageDate] = _currentdate - 1
                && [userName] = _currentname
                && [productName] = _currentproduct
        )
    )
RETURN
    IF ( _previoushour > 20, _previoustoken, 0 )

result.png

Here is the sample file hopes to help you, please try it: PBIX 

 

Best Regards,
Yingjie Li

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

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.