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
blisbao
Regular Visitor

Compare rows and sum value

Hello to all,

 

I'm using Power BI to analyse a very large database and I'm stuck in a situation.

 

I have a table with time periods and amounts for each item. Each row has a start and stop time.

 

I need to summarize all the continuous time periods. For each name, I need to combine rows where the stop time is equal the next start time (continuous work) and sum their amount column.

 

a.JPG

 

SOURCE DATA:

NameStartStopAmount
A14/07/2019 10:02:3114/07/2019 10:05:2095
A17/07/2019 07:05:5517/07/2019 07:08:0870
A17/07/2019 07:18:4317/07/2019 07:33:43862
A17/07/2019 07:33:4317/07/2019 07:36:30137
A17/07/2019 07:37:3417/07/2019 07:39:53108
B15/07/2019 07:30:1315/07/2019 07:31:1214
B15/07/2019 07:32:3715/07/2019 07:47:37900
B15/07/2019 07:47:3715/07/2019 08:02:37900
B15/07/2019 08:02:3715/07/2019 08:17:37849
B15/07/2019 08:17:3715/07/2019 08:20:38150
B15/07/2019 08:21:5615/07/2019 08:23:219
B15/07/2019 08:23:3015/07/2019 08:38:30899

 

 

DESIRED RESULT:

 

NameStartStopAmount
A14/07/2019 10:02:3114/07/2019 10:05:2095
A17/07/2019 07:05:5517/07/2019 07:08:0870
A17/07/2019 07:18:4317/07/2019 07:36:30999
A17/07/2019 07:37:3417/07/2019 07:39:53108
B15/07/2019 07:30:1315/07/2019 07:31:1214
B15/07/2019 07:32:3715/07/2019 08:20:382799
B15/07/2019 08:21:5615/07/2019 08:23:219
B15/07/2019 08:23:3015/07/2019 08:38:30899

 

 

Thanks in advance.

1 ACCEPTED SOLUTION
lc_finance
Solution Sage
Solution Sage

Hi @blisbao ,

 

 

You can download my proposed solution from here.

Here is how I would approach it:

 

1) add an Index column. This allows you to clearly identify each row. You can do it in Power Query Editor by going to Add Column -> Index Column

Add Index column.png

 

2) Add a calculated column to identify the Real Start. Only rows that are the first in a sequence have a ' Real Start'.

Here is the DAX formula:

 

Real Start = 

VAR currentStart = [Start]

VAR isRealStart = COUNTX(
    FILTER('Transactions',
    'Transactions'[Stop]=currentStart)
, [Stop]) = BLANK()


RETURN IF(isRealStart, currentStart, BLANK())

Here is the result:

Real Start.png

3) Add a column to identify the index of the row with the real start.

Here is the DAX formula:

Real Start Index = 

VAR isRealStart = NOT [Real Start]= BLANK()
VAR currentIndex = [Index]

VAR realStartIndex = MAXX(
    FILTER('Transactions', AND( [Index]<=currentIndex, NOT [Real Start] = BLANK()))
, [Index])

RETURN realStartIndex

and here is the result:

Real Start Index.png

 

Now you can repeat the same for the Stop.

Finally, add a calculated column with the sum of the amounts only if the row is the 'Real Start' of the sequence. You can find the formulas for these additional measures in the solution Power BI file.

 

To obtain exactly your 'desired result' table, you filter out empty rows in the column ' Real Start' .

 

Does this help you? Do not hesitate if you have further questions.

 

LC

Interested in Power BI and DAX templates? Check out my blog at www.finance-bi.com

View solution in original post

1 REPLY 1
lc_finance
Solution Sage
Solution Sage

Hi @blisbao ,

 

 

You can download my proposed solution from here.

Here is how I would approach it:

 

1) add an Index column. This allows you to clearly identify each row. You can do it in Power Query Editor by going to Add Column -> Index Column

Add Index column.png

 

2) Add a calculated column to identify the Real Start. Only rows that are the first in a sequence have a ' Real Start'.

Here is the DAX formula:

 

Real Start = 

VAR currentStart = [Start]

VAR isRealStart = COUNTX(
    FILTER('Transactions',
    'Transactions'[Stop]=currentStart)
, [Stop]) = BLANK()


RETURN IF(isRealStart, currentStart, BLANK())

Here is the result:

Real Start.png

3) Add a column to identify the index of the row with the real start.

Here is the DAX formula:

Real Start Index = 

VAR isRealStart = NOT [Real Start]= BLANK()
VAR currentIndex = [Index]

VAR realStartIndex = MAXX(
    FILTER('Transactions', AND( [Index]<=currentIndex, NOT [Real Start] = BLANK()))
, [Index])

RETURN realStartIndex

and here is the result:

Real Start Index.png

 

Now you can repeat the same for the Stop.

Finally, add a calculated column with the sum of the amounts only if the row is the 'Real Start' of the sequence. You can find the formulas for these additional measures in the solution Power BI file.

 

To obtain exactly your 'desired result' table, you filter out empty rows in the column ' Real Start' .

 

Does this help you? Do not hesitate if you have further questions.

 

LC

Interested in Power BI and DAX templates? Check out my blog at www.finance-bi.com

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.