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
ChrisLoan
Helper I
Helper I

How to check if one value of a combination of values has changed over time

Hi Everyone!

 

I have a question for my fellow BI people. I'm gonna use dummy data to give an idea of our Power BI dataset:

 

So we have a Fact table Invoices with many columns, but the most important ones for my questions are:

 

  • Invoice Number
  • Worker Key
  • Customer Key
  • Date
  • IsPayroll?

To give you a visual representation:

 

ChrisLoan_1-1655122675354.png

 

 

My company wants a new column (based on a condition), that has 3 options; SET payroll, NO payroll, VARIABLE payroll.

 

They wanna know if a certain combination, for example above invoice nr 1 with customer key 20 and worker key 55, has changed it's value in the column "ispayroll" from "n" to "y" over the period of 20 days. If that's the case the new column must be set to "variable". If a combination starts out for the first time with "ispayroll" "Y" the new column value must be set to "Payroll set". If the  "ispayroll" column has a "n" value the new column value must be set to "no payroll". So the result from the example above should be filled in like this:

 

ChrisLoan_2-1655122746423.png

 

 

So the condition must be checked based on the combination of Invoice Nr, Customer Key, Worker Key and Date.

 

Can anyone help me? I know this might be a little difficult to understand, but any help is welcome! 😉

1 ACCEPTED SOLUTION

Hi, @ChrisLoan 

 

You can try the following methods.

Result =
IF (
    [payroll?] = "y"
        && [date]
            = CALCULATE (
                MIN ( 'Table'[date] ),
                FILTER (
                    'Table',
                    [Invoice number] = EARLIER ( 'Table'[Invoice number] )
                        && [Customer key] = EARLIER ( 'Table'[Customer key] )
                        && [worker key] = EARLIER ( 'Table'[worker key] )
                )
            ),
    "payroll set",
    IF (
        [payroll?] = "n"
            && [date]
                = CALCULATE (
                    MIN ( 'Table'[date] ),
                    FILTER (
                        'Table',
                        [Invoice number] = EARLIER ( 'Table'[Invoice number] )
                            && [Customer key] = EARLIER ( 'Table'[Customer key] )
                            && [worker key] = EARLIER ( 'Table'[worker key] )
                    )
                ),
        "no payroll",
        IF (
            [payroll?] = "y"
                && [date]
                    >= CALCULATE (
                        MIN ( 'Table'[date] ),
                        FILTER (
                            'Table',
                            [Invoice number] = EARLIER ( 'Table'[Invoice number] )
                                && [Customer key] = EARLIER ( 'Table'[Customer key] )
                                && [worker key] = EARLIER ( 'Table'[worker key] )
                        )
                    ) + 20,
            "variable",
            IF ( [payroll?] = "n", "no payroll" )
        )
    )
)

This time the formula is written based on the invoice number, the worker key and the customer key.

vzhangti_1-1655790654272.png

(3, 40, 77) obtained the correct value.

 

As shown in the graph, (1,20,55) has been rejected for the first time after 20 days on March 1, shouldn't it be "variable"? If there are other conditions, please let me know.

vzhangti_0-1655790610390.png

 

Best Regards,

Community Support Team _Charlotte

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

9 REPLIES 9
v-zhangti
Community Support
Community Support

Hi, @ChrisLoan 

 

You can try the following methods.

Result = 
IF (
    [payroll?] = "y"
        && [date]
            = CALCULATE (
                MIN ( 'Table'[date] ),
                FILTER ( 'Table', [Invoice number] = EARLIER ( 'Table'[Invoice number] ) )
            ),
    "payroll set",
    IF (
        [payroll?] = "n"
            && [date]
                = CALCULATE (
                    MIN ( 'Table'[date] ),
                    FILTER ( 'Table', [Invoice number] = EARLIER ( 'Table'[Invoice number] ) )
                ),
        "no payroll",
        IF (
            [payroll?] = "y"
                && [date]
                    > CALCULATE (
                        MIN ( 'Table'[date] ),
                        FILTER ( 'Table', [Invoice number] = EARLIER ( 'Table'[Invoice number] ) )
                    ) + 20,
            "variable",
            IF ( [payroll?] = "n", "no payroll" )
        )
    )
)

vzhangti_0-1655368203525.png

Is this the result you expect?

 

Best Regards,

Community Support Team _Charlotte

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

Also, the check has to be based off the combination of invoice number, worker key and customer key. If I add a new line with invoice number  1 but with a different worker key and customer key, it fills it in with "Variable" as well, when in fact it should fill it in with 'Payroll Set':

 

ChrisLoan_0-1655379373034.png

 

So sorry, I know what I'm asking is very complicated!

Hi, @ChrisLoan 

 

You can try the following methods.

Result =
IF (
    [payroll?] = "y"
        && [date]
            = CALCULATE (
                MIN ( 'Table'[date] ),
                FILTER (
                    'Table',
                    [Invoice number] = EARLIER ( 'Table'[Invoice number] )
                        && [Customer key] = EARLIER ( 'Table'[Customer key] )
                        && [worker key] = EARLIER ( 'Table'[worker key] )
                )
            ),
    "payroll set",
    IF (
        [payroll?] = "n"
            && [date]
                = CALCULATE (
                    MIN ( 'Table'[date] ),
                    FILTER (
                        'Table',
                        [Invoice number] = EARLIER ( 'Table'[Invoice number] )
                            && [Customer key] = EARLIER ( 'Table'[Customer key] )
                            && [worker key] = EARLIER ( 'Table'[worker key] )
                    )
                ),
        "no payroll",
        IF (
            [payroll?] = "y"
                && [date]
                    >= CALCULATE (
                        MIN ( 'Table'[date] ),
                        FILTER (
                            'Table',
                            [Invoice number] = EARLIER ( 'Table'[Invoice number] )
                                && [Customer key] = EARLIER ( 'Table'[Customer key] )
                                && [worker key] = EARLIER ( 'Table'[worker key] )
                        )
                    ) + 20,
            "variable",
            IF ( [payroll?] = "n", "no payroll" )
        )
    )
)

This time the formula is written based on the invoice number, the worker key and the customer key.

vzhangti_1-1655790654272.png

(3, 40, 77) obtained the correct value.

 

As shown in the graph, (1,20,55) has been rejected for the first time after 20 days on March 1, shouldn't it be "variable"? If there are other conditions, please let me know.

vzhangti_0-1655790610390.png

 

Best Regards,

Community Support Team _Charlotte

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

Hi!

 

It works! This is exactly what I needed, thanks! 😊

 

And you're right, it should be "Variable" for that example, I highlighted the wrong row in my screenshot sorry. I meant to highlight the row above, which had the same invoice number, but different customer key and worker key. But that issue has been resolved with the new code you sent me so no worries! 👍

 

I'm gonna try to implement this in our actual dataset and hopefully everything will work exactly as it does with the dummy data we used here!

Hi!

 

Thanks so much for the code, it works and is almost perfect! But unfortunately not entirely as it needs to when I add more data to the dummy data:

 

ChrisLoan_0-1655373915730.png

As you can see in the example above, there's a blank value? This should have also been filled in with "Variable" as the combination (3, 40, 77) passed 20 days after it's first entry? We're getting close though, this is very promising and almost exactly what I need! 😊

Greg_Deckler
Super User
Super User

@ChrisLoan Try this:

Column =
  VAR __Invoice = [Invoice number]
  VAR __Worker = [worker key]
  VAR __Customer = [customer key]
  VAR __Table = FILTER('Table',[Invoice number] = __Invoice && [worker key] = __Worker && [customer key] = __Customer)
  VAR __Count = COUNTROWS(DISTINCT(SELECTCOLUMNS(__Table,"__Payroll",[payroll?])))
  VAR __Payroll = MAXX(DISTINCT(SELECTCOLUMNS(__Table,"__Payroll",[payroll?])),[__Payroll])
RETURN
  SWITCH(TRUE(),
    __Count > 1, "Variable",
    __Payroll = "n", "No payroll",
    "Payroll set"
  )

@ 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...

Hey Greg

 

Is there a way to add the 20 days rule into account in this code? 🙂

Sorry for the delay, I've been sick and haven't been able to work.

 

This doesn't work for my questions because it doesn't take in account the 20 day rule that is needed for the "Variable" payroll option.

 

But it is a nice start because it does a lot of things right! Maybe there's something we can do to add the 20 day rule to your code? 

Hey Greg!

 

Thanks so much for the quick response, I'll try the code out tomorrow! 😊

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