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

Calculating difference between two values in one column based on date and business line

Dear Community,

 

I have the following (excel) dataset where I would like to calculate the difference of the Actual column between months and per business line. I am fairly new to Power BI, therefore, given the way the data is structured, I still can't figure out a way to create a column/measure to get the desired output Difference.  In the Actual column, data is only filled for the months from January to August, while the remaining months are blank for now and will be updated in the excel over time.

 

DataCapture.PNG

 

I hope you can help with answering this question and thanks a lot for your help!

1 ACCEPTED SOLUTION
AlB
Super User
Super User

Hi @Anonymous

First of all, please always show your sample data in text-tabular format in addition to (or instead of) the screen captures. A screen cap doesn't allow people to readily copy the data and run a quick test and thus decreases the likelihood of your question being answered. Just use 'Copy table' in Power BI and paste it here. Or, ideally, share the pbix (beware of confidential data).

Try this for a new calculated column in your table. Haven't tested it for lack of data in the proper format: 

Difference =
VAR PreviousDate_ =
    CALCULATE (
        MAX ( Table1[Month] ),
        Table1[Month] < EARLIER ( Table1[Month] ),
        ALLEXCEPT ( Table1, Table1[Business Line] )
    )
VAR PreviousValue_ =
    CALCULATE (
        DISTINCT ( Table1[Actual] ),
        Table1[Month] = PreviousDate_,
        ALLEXCEPT ( Table1, Table1[Business Line] )
    )
VAR CurrentValue_ = Table1[Actual]
RETURN
    IF (
        NOT ISBLANK ( CurrentValue_ ) && NOT ISBLANK ( PreviousValue_ ),
        CurrentValue_ - PreviousValue_
    )

 

View solution in original post

6 REPLIES 6
AlB
Super User
Super User

Hi @Anonymous

First of all, please always show your sample data in text-tabular format in addition to (or instead of) the screen captures. A screen cap doesn't allow people to readily copy the data and run a quick test and thus decreases the likelihood of your question being answered. Just use 'Copy table' in Power BI and paste it here. Or, ideally, share the pbix (beware of confidential data).

Try this for a new calculated column in your table. Haven't tested it for lack of data in the proper format: 

Difference =
VAR PreviousDate_ =
    CALCULATE (
        MAX ( Table1[Month] ),
        Table1[Month] < EARLIER ( Table1[Month] ),
        ALLEXCEPT ( Table1, Table1[Business Line] )
    )
VAR PreviousValue_ =
    CALCULATE (
        DISTINCT ( Table1[Actual] ),
        Table1[Month] = PreviousDate_,
        ALLEXCEPT ( Table1, Table1[Business Line] )
    )
VAR CurrentValue_ = Table1[Actual]
RETURN
    IF (
        NOT ISBLANK ( CurrentValue_ ) && NOT ISBLANK ( PreviousValue_ ),
        CurrentValue_ - PreviousValue_
    )

 

Thank you! I have been looking for this approach. I've used it to calculate first derivative. 

Any example report with this calculation you can share im triying to follow the isntruccion but i can not get it 

Anonymous
Not applicable

Thank you so much @AlB , the solution worked! Apologies for not including the sample data in the correct format, please find this below.

 

Business LineMonthActual
A01/01/201910
A01/02/201920
A01/03/201940
A01/04/201970
A01/05/2019100
A01/06/2019120
A01/07/2019130
A01/08/2019150
A01/09/2019 
A01/10/2019 
A01/11/2019 
A01/12/2019 
B01/01/20195
B01/02/20197
B01/03/20199
B01/04/20198
B01/05/201910
B01/06/201911
B01/07/201915
B01/08/201918
B01/09/2019 
B01/10/2019 
B01/11/2019 
B01/12/2019 
C01/01/2019100
C01/02/201980
C01/03/201965
C01/04/201950
C01/05/201945
C01/06/201940
C01/07/201935
C01/08/201925
C01/09/2019 
C01/10/2019 
C01/11/2019 
C01/12/2019 

So this looked like it might work for my own situation, and I tried it. Of course, just as with anything in this environment, copying a solution is a dangerous thing, especially for those of us who don't understand what the solution is doing. If you are a person who is happy that the solution worked and could care less about what's "under the hood," then this type of solution is good for you. 

For those of us who are not DAX or PowerQuery wizards, it would be helpful to provide a couple of notes to let us know what some of these functions do. Why, for instance, is the DISTINCT function needed; and what does the ALLEXCEPT function do? I ask because I tried this solution and as soon as I hit the ENTER key, I got an error "A table of multiple values was supplied where a single value was expected." Because I only partially understand what this code was supposed to do, I have no idea how to begin troubleshooting much less fixing the problem. The only real difference I can see between the structure of the data I am using (GitHub COVID-19 Confirmed Case time series data) and the example given is that in my data, there are no blank cells in the value column ("Cases" in my dataset, "Actuals" in the example given here). Other than that, I think everything is about the same. I have a column of dates, a column of locations (Counties/locations encoded by using FIPS codes) that fulfills the same function as the "Business Line" function in the example, and a column of numeric values I want to calculate differences for. 

I am trying to get PowerBI to calculate the difference between the case count for a specific location on one date and the case count for the same location on the day before - essentially, "New Cases." Can anyone help? I can provide a link to the dataset if needed...https://github.com/CSSEGISandData/COVID-19/blob/master/csse_covid_19_data/csse_covid_19_time_series/... (hint: to bring this into PowerBI, you can go to a Web datasource, navigate to this page, right-click the "Download" link, copy the link address, and paste it into the box in the PowerBI dialog). 

Hey, any luck on solving this?

 

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.