cancel
Showing results for 
Search instead for 
Did you mean: 

Fabric is Generally Available. Browse Fabric Presentations. Work towards your Fabric certification with the Cloud Skills Challenge.

Reply
Anonymous
Not applicable

Create date difference value column ?

Hello, 

 

I want to create a column like created column( current date - last date by country) in power bi. 

I have country/Region, date and death column. 

 

How do I create created column? 

 

Country/RegionDateDeathsCreated column
Brazil24-Apr-21   389,492    3,076
Brazil23-Apr-21   386,416    2,914
Brazil22-Apr-21   383,502    2,027
Brazil21-Apr-21   381,475    3,472
Brazil20-Apr-21   378,003    0
Argentina 24-Apr-21   432,312     90
Argentina 23-Apr-21   432,222     679
Argentina 22-Apr-21   431,543    309
Argentina 21-Apr-21   431,234    1,000
Argentina 20-Apr-21   430,234    0

 

thanks in advance

1 ACCEPTED SOLUTION

Hi @Anonymous 

 

Try this code to add a new column : 

Column = 
VAR _CD = 'Table'[Date]
VAR _YD =
    CALCULATE (
        MAX ( 'Table'[Date] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[Date] < _CD
                && 'Table'[Country/Region] = EARLIER ( 'Table'[Country/Region] )
        )
    )
VAR _YDV =
    CALCULATE (
        MAX ( 'Table'[Deaths] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[Date] = _YD
                && 'Table'[Country/Region] = EARLIER ( 'Table'[Country/Region] )
        )
    )
RETURN
    IF ( ISBLANK ( _YD ), 0, 'Table'[Deaths] - _YDV )

 

output:

 

VahidDM_0-1642375533905.png

 

 

If this post helps, please consider accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudos!!
LinkedIn: 
www.linkedin.com/in/vahid-dm/

 

 

View solution in original post

7 REPLIES 7

Hi @Anonymous 

 

Try this code to add a new column : 

Column = 
VAR _CD = 'Table'[Date]
VAR _YD =
    CALCULATE (
        MAX ( 'Table'[Date] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[Date] < _CD
                && 'Table'[Country/Region] = EARLIER ( 'Table'[Country/Region] )
        )
    )
VAR _YDV =
    CALCULATE (
        MAX ( 'Table'[Deaths] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[Date] = _YD
                && 'Table'[Country/Region] = EARLIER ( 'Table'[Country/Region] )
        )
    )
RETURN
    IF ( ISBLANK ( _YD ), 0, 'Table'[Deaths] - _YDV )

 

output:

 

VahidDM_0-1642375533905.png

 

 

If this post helps, please consider accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudos!!
LinkedIn: 
www.linkedin.com/in/vahid-dm/

 

 

AlexisOlson
Super User
Super User

If you want to do this in the query editor, have a look at my previous solution here:
https://community.powerbi.com/t5/Power-Query/Optimize-performance-at-Un-Cumulate-calculation/m-p/219...

 

You may want to review this simpler case explained with more detail first:
https://community.powerbi.com/t5/Desktop/Calculating-Daily-values-from-Cumulative-Total/m-p/2198969

Anonymous
Not applicable

Hello,

 

Your solution might not work as we have to take the country into the account as well. 

 

thanks 

 

The first one I linked to does handle this. The second one is the simpler example that does not.

Anonymous
Not applicable

Sure thanks, will take a look. 

 

ValtteriN
Super User
Super User

Hi,

Here is one way to this:


EarlierDatediff =
var _country = 'Table (5)'[Country/Region]
var _edate = CALCULATE(MAX('Table (5)'[Date]),FILTER('Table (5)','Table (5)'[Date] <EARLIER('Table (5)'[Date])),'Table (5)'[Country/Region]=_country)
var _datediff = DATEDIFF('Table (5)'[Date],_edate,DAY)
return

_datediff
End result (I modified 1 date to demo -2 in Argentina):
ValtteriN_0-1642357490285.png


I hope this post helps to solve your issue and if it does consider accepting it as a solution and giving the post a thumbs up!





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Anonymous
Not applicable

Hello, 

 

Sorry, let me rephrase the problem. 

 

I have to create a column with current date value - previous date value by country. 

 

your solution is only giving the datediff. 

 

thanks 

 

 

Helpful resources

Announcements
PBI November 2023 Update Carousel

Power BI Monthly Update - November 2023

Check out the November 2023 Power BI update to learn about new features.

Community News

Fabric Community News unified experience

Read the latest Fabric Community announcements, including updates on Power BI, Synapse, Data Factory and Data Activator.

Dashboard in a day with date

Exclusive opportunity for Women!

Join us for a free, hands-on Microsoft workshop led by women trainers for women where you will learn how to build a Dashboard in a Day!

Power BI Fabric Summit Carousel

The largest Power BI and Fabric virtual conference

130+ sessions, 130+ speakers, Product managers, MVPs, and experts. All about Power BI and Fabric. Attend online or watch the recordings.

Top Solution Authors
Top Kudoed Authors