Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
qhpowerbi
Helper III
Helper III

[DAX] Different between row based on column value

Hi all, 

 I have the electric reading data as below. I am looking to calculate the consumption of electric every month based on level. the logic is [ reading current month - reading previous month = current month consumption].

 

My question is how can i create the dax based on data I have as below; 

 

qhpowerbi_0-1662085501377.png


thanks in advance for help!



1 ACCEPTED SOLUTION
v-yiruan-msft
Community Support
Community Support

Hi  @qhpowerbi ,

I created a sample pbix file(see attachment) for you, please check whether that is what you want. You can create a calculated column or measure as below:

calculated column:

Column = 
VAR _year =
    IF ( 'Table'[Month] = 1, 'Table'[Year] - 1, 'Table'[Year] )
VAR _month =
    IF ( 'Table'[Month] = 1, 12, 'Table'[Month] - 1 )
VAR _premreading =
    CALCULATE (
        SUM ( 'Table'[reading] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            'Table'[Level] = EARLIER('Table'[Level])
                && 'Table'[Year] = _year
                && 'Table'[Month] = _month
        )
    )
RETURN
    IF(ISBLANK(_premreading),BLANK(),'Table'[reading]-_premreading)

yingyinr_0-1662453548169.png

Measure:

consumption = 
VAR _curlevel =
    SELECTEDVALUE ( 'Table'[Level] )
VAR _curyear =
    SELECTEDVALUE ( 'Table'[Year] )
VAR _curmonth =
    SELECTEDVALUE ( 'Table'[Month] )
VAR _year =
    IF ( _curmonth = 1, _curyear - 1, _curyear )
VAR _month =
    IF ( _curmonth = 1, 12, _curmonth - 1 )
VAR _premreading =
    CALCULATE (
        SUM ( 'Table'[reading] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            'Table'[Level] = _curlevel
                && 'Table'[Year] = _year
                && 'Table'[Month] = _month
        )
    )
RETURN
    IF(ISBLANK(_premreading),BLANK(),SUM('Table'[reading])-_premreading)

If the above one can't help you get the desired result, please provide some sample data in your tables (exclude sensitive data) with Text format and your expected result with backend logic and special examples. It is better if you can share a simplified pbix file. You can refer the following link to upload the file to the community. Thank you.

How to upload PBI in Community

Best Regards

Community Support Team _ Rena
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

3 REPLIES 3
v-yiruan-msft
Community Support
Community Support

Hi  @qhpowerbi ,

I created a sample pbix file(see attachment) for you, please check whether that is what you want. You can create a calculated column or measure as below:

calculated column:

Column = 
VAR _year =
    IF ( 'Table'[Month] = 1, 'Table'[Year] - 1, 'Table'[Year] )
VAR _month =
    IF ( 'Table'[Month] = 1, 12, 'Table'[Month] - 1 )
VAR _premreading =
    CALCULATE (
        SUM ( 'Table'[reading] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            'Table'[Level] = EARLIER('Table'[Level])
                && 'Table'[Year] = _year
                && 'Table'[Month] = _month
        )
    )
RETURN
    IF(ISBLANK(_premreading),BLANK(),'Table'[reading]-_premreading)

yingyinr_0-1662453548169.png

Measure:

consumption = 
VAR _curlevel =
    SELECTEDVALUE ( 'Table'[Level] )
VAR _curyear =
    SELECTEDVALUE ( 'Table'[Year] )
VAR _curmonth =
    SELECTEDVALUE ( 'Table'[Month] )
VAR _year =
    IF ( _curmonth = 1, _curyear - 1, _curyear )
VAR _month =
    IF ( _curmonth = 1, 12, _curmonth - 1 )
VAR _premreading =
    CALCULATE (
        SUM ( 'Table'[reading] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            'Table'[Level] = _curlevel
                && 'Table'[Year] = _year
                && 'Table'[Month] = _month
        )
    )
RETURN
    IF(ISBLANK(_premreading),BLANK(),SUM('Table'[reading])-_premreading)

If the above one can't help you get the desired result, please provide some sample data in your tables (exclude sensitive data) with Text format and your expected result with backend logic and special examples. It is better if you can share a simplified pbix file. You can refer the following link to upload the file to the community. Thank you.

How to upload PBI in Community

Best Regards

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
MahyarTF
Memorable Member
Memorable Member

Hi,

Create a Index in power Query, then create below Column in particular table :

_Consumption =
Var _CurrentIndex = Sheet150[Index]
Var _CurrentLevel = Sheet150[Level]
Var _previousIndex = CALCULATE( max(Sheet150[Index]),
                                filter(Sheet150, Sheet150[Index] < _CurrentIndex),
                                filter(sheet150, Sheet150[Level] = _CurrentLevel),
                                ALLEXCEPT(Sheet150,sheet150[level],Sheet150[Month Name], Sheet150[Month], Sheet150[Year])
                            )
var _PreviousReading = CALCULATE( max(Sheet150[reading]), filter(Sheet150, Sheet150[Index] = _previousIndex))
return Sheet150[reading] - _PreviousReading
this is the result : 
MahyarTF_0-1662088610212.png

 

Mahyartf
amitchandak
Super User
Super User

@qhpowerbi , Create a date with help from month and year, and join it with date of date table and use TI for meaures

 

Date = date([year], [month],1)

 

MTD Sales = CALCULATE(SUM(Table[reading]),DATESMTD('Date'[Date]))
last MTD Sales = CALCULATE(SUM(Table[reading]),DATESMTD(dateadd('Date'[Date],-1,MONTH)))
last month Sales = CALCULATE(SUM(Table[reading]),previousmonth('Date'[Date]))

 

 

Power BI — Month on Month with or Without Time Intelligence
https://medium.com/@amitchandak.1978/power-bi-mtd-questions-time-intelligence-3-5-64b0b4a4090e

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

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.