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
DanielPasalic
Helper II
Helper II

How to calculate differences in values between dates in rows within the same column

Hi all I have a problem to My tableMy tableDAX formula that did not workedDAX formula that did not worked.  There are 4 different columns: - date, id, Art.nr. and sold_item I would like to calculate differences in sold_item for products with same ID between dates (starting from oldest to latest date).  I tried in DAX with the calculated column but did not work.  Anyone who can give me advice on how to do it?

5 REPLIES 5
Anonymous
Not applicable

Should this not be calculated in Power Query? If this is not a dynamic calculation (and I doubt it is), please do it in PQ. Simpler, faster, more efficient.

sorry but I have no idea how to do it in Power Query

Anonymous
Not applicable

// This should be calculated in Power Query.
// I give you a DAX formula but if the table is
// big (and it seems to be a fact table), you
// might wait a very long time for this calculation
// to end. Depending on the size of the table
// you might even get a memory overflow. Then
// you know you have to do it in PQ. Let's say
// your table is T (I don't want to type the full name).
// By the way, using CALCULATE for a row-by-row
// calculation in a fact table will get you in
// trouble in no time.

sales_diff = // calc column
var __artikelNr = T[Artikelnr]
var __firstValue =
    SUMX(
        // I can use topn(1,...) here because
        // all rows with the same dates (ties)
        // will be returned. Same goes for the
        // other expression.
        topn(1,
            filter(
                T,
                T[Artikelnr] = __artikelNr
            ),
            T[Date],
            ASC
        ),
        T[Salt antal totalt]
    )
var __secondValue =
    SUMX(
        topn(1,
            filter(
                T,
                T[Artikelnr] = __artikelNr
            ),
            T[Date],
            DESC
        ),
        T[Salt antal totalt]
    )    
return
    __firstValue - __secondValue
harshnathani
Community Champion
Community Champion

Hi @DanielPasalic ,

 

https://community.powerbi.com/t5/Community-Blog/Getting-Previous-Values-in-Power-BI-Part-1/ba-p/1143...

 

Regards,

Harsh Nathani
Did I answer your question? Mark my post as a solution! Appreciate with a Kudos!! (Click the Thumbs Up Button)

Greg_Deckler
Super User
Super User

@DanielPasalic  - See my article on Mean Time Between Failure (MTBF) which uses EARLIER: http://community.powerbi.com/t5/Community-Blog/Mean-Time-Between-Failure-MTBF-and-Power-BI/ba-p/3395...


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

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.