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

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a $100 discount. Register Now

Reply
JeroenR
Helper V
Helper V

get value from previous row

Hi all,

 

I have the following table structure within power bi

DateTimeINOUTINSIDE
28/11/2022 11:00:001 1
28/11/2022 12:00:00322
28/11/2022 13:00:00451
28/11/2022 14:00:00212

 

Is it possible to calculate the INSIDE column within power bi via a measure or calculated column?

When doing this in Excel it is very easy to calculate this, just take one cell from above and then add this number with the outcome of the IN - OUT calculation. That would be something like this in Excel (where C stands for INSIDE, A for IN and B for OUT.): 

=C1 + (A2 - B2) 

  

2 ACCEPTED SOLUTIONS
v-yueyunzh-msft
Community Support
Community Support

Hi , @JeroenR 

According to your description, you want to "get value from previous row".

First , you need to sort the [DateTime] in Power Query and you need to check the [DateTime] is single value in each row. IF not , you need to add an index column in Power Query Editor as a row number to judge .

Here, i use your [DateTime] as a row number to check which row is calculating.

Then we can click "New Column" to add a calculated column by this dax code:

Column = var _cur_row = [DateTime]
var _t =ADDCOLUMNS( FILTER('Table' , 'Table'[DateTime]<= _cur_row) , "cal" , [IN] - [OUT])
return
SUMX(_t ,[cal])

Then we can meet your need:

vyueyunzhmsft_0-1677203638990.png

Thank you for your time and sharing, and thank you for your support and understanding of PowerBI! 

 

Best Regards,

Aniya Zhang

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

FreemanZ
Super User
Super User

hi @JeroenR 

you may also add a calculated column like:

INSIDE2 = 
SUMX(
    FILTER(
        TableName, 
        TableName[DateTime]<=EARLIER(TableName[DateTime])
    ),
    TableName[IN] - TableName[OUT]
)

 

it worked like:

FreemanZ_0-1677218029135.png

View solution in original post

3 REPLIES 3
JeroenR
Helper V
Helper V

Thank you both @v-yueyunzh-msft and @FreemanZ . Both of you solutions works very good and do the job in a much better way than what I had created myself!

 

I have made a small adjustment to the code so that it resets the counts to 0 every next day and also added a function that it prevents the number to be below 0. See the code below:

 

INSIDE = 
MAX(0,
    SUMX(
        FILTER(
            Footfall,
            'Footfall'[Date.1] = EARLIER('Footfall'[Date.1]) &&
            Footfall[Date / time] <= EARLIER(Footfall[Date / time])
        ),
        Footfall[bezoekers(In)] - Footfall[bezoekers(Out)]
    )
)

 

 

Thank you again both for your solution and help!

FreemanZ
Super User
Super User

hi @JeroenR 

you may also add a calculated column like:

INSIDE2 = 
SUMX(
    FILTER(
        TableName, 
        TableName[DateTime]<=EARLIER(TableName[DateTime])
    ),
    TableName[IN] - TableName[OUT]
)

 

it worked like:

FreemanZ_0-1677218029135.png

v-yueyunzh-msft
Community Support
Community Support

Hi , @JeroenR 

According to your description, you want to "get value from previous row".

First , you need to sort the [DateTime] in Power Query and you need to check the [DateTime] is single value in each row. IF not , you need to add an index column in Power Query Editor as a row number to judge .

Here, i use your [DateTime] as a row number to check which row is calculating.

Then we can click "New Column" to add a calculated column by this dax code:

Column = var _cur_row = [DateTime]
var _t =ADDCOLUMNS( FILTER('Table' , 'Table'[DateTime]<= _cur_row) , "cal" , [IN] - [OUT])
return
SUMX(_t ,[cal])

Then we can meet your need:

vyueyunzhmsft_0-1677203638990.png

Thank you for your time and sharing, and thank you for your support and understanding of PowerBI! 

 

Best Regards,

Aniya Zhang

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

Helpful resources

Announcements
Fabric Community Conference

Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

February 2024 Update Carousel

Power BI Monthly Update - February 2024

Check out the February 2024 Power BI update to learn about new features.

Fabric Career Hub

Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

Fabric Partner Community

Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.

Top Solution Authors