Forum Discussion
Calculate between rows
Good afternoon
Surely it is a very simple problem, but I do not find the right solution. I have a database, which brings me a record of samples as shown in the table.
| Date | Name | Code | Value |
| 01/01/2021 | Water meter | 2235 | 30000 |
| 01/01/2021 | Electricity meter | 3358 | 1235 |
| 01/01/2021 | Contador de gas | 6589 | 232 |
| 01/01/2021 | Test | 9999 | Yes |
| 02/01/2021 | Water meter | 2235 | 30256 |
| 02/01/2021 | Electricity meter | 3358 | 1302 |
| 02/01/2021 | Contador de gas | 6589 | 236 |
| 03/01/2021 | Water meter | 2235 | 30423 |
| 03/01/2021 | Electricity meter | 3358 | 1406 |
| 03/01/2021 | Contador de gas | 6589 | 238 |
| 04/01/2021 | Test | 9999 | No |
| 04/01/2021 | Water meter | 2235 | 30500 |
| 04/01/2021 | Electricity meter | 3358 | 1455 |
| 04/01/2021 | Contador de gas | 6589 | 236 |
I need to add a column with the water consumption of each day, the electricity consumption and the gas consumption. I do not know how I could filter it to get the result to appear on 02/01/2021 (30256-30000), that is, 256, the consumption of that day, something such that
| Date | Name | Code | Value | Cons water | Cons Elect | Cons gas |
| 01/01/2021 | Water meter | 2235 | 30000 | |||
| 01/01/2021 | Electricity meter | 3358 | 1235 | |||
| 01/01/2021 | Contador de gas | 6589 | 232 | |||
| 01/01/2021 | Test | 9999 | Yes | |||
| 02/01/2021 | Water meter | 2235 | 30256 | 256 | ||
| 02/01/2021 | Electricity meter | 3358 | 1302 | 67 | ||
| 02/01/2021 | Contador de gas | 6589 | 236 | 4 | ||
| 03/01/2021 | Water meter | 2235 | 30423 | 167 | ||
| 03/01/2021 | Electricity meter | 3358 | 1406 | 104 | ||
| 03/01/2021 | Contador de gas | 6589 | 238 | 2 | ||
| 04/01/2021 | Test | 9999 | No | |||
| 04/01/2021 | Water meter | 2235 | 30500 | 77 | ||
| 04/01/2021 | Electricity meter | 3358 | 1455 | 49 | ||
| 04/01/2021 | Contador de gas | 6589 | 245 | 7 |
Obviously, it is just a sample, the database consists of more than 200 different codes, and there is data from several years.
Could you help me? Thank you very much in advance!
- Anonymous4 years ago
Hi Syndicate_Admin ,
Please refer this formula:
cons water = var last_date = CALCULATE(MAX('Table'[Date]),FILTER('Table','Table'[Date]<EARLIER('Table'[Date])&&'Table'[Code]=2235)) var last_value = CALCULATE(MAX('Table'[Value]),FILTER('Table','Table'[Date]=last_date&&'Table'[Code]=2235)) return IF(ISBLANK(last_value),BLANK(),IF('Table'[Code]=2235,'Table'[Value]-last_value))For [Cons Elect] and [Cons gas], just need to replace the 'Table'[Code] in the formula.
Result:
Best Regards,
Jay
2 Replies
- johnt75Super User
Rather than adding 200 columns I would create a summary table with 1 entry per day per code, like
Summary Table = SUMMARIZECOLUMNS( 'Table'[Date], 'Table'[Code], 'Table'[Value], "Consumption", VAR currentDate = SELECTEDVALUE( 'Table'[Date] ) VAR currentCode = SELECTEDVALUE( 'Table'[Code] ) VAR currentValue = SELECTEDVALUE( 'Table'[Value] ) VAR prevValue = SELECTCOLUMNS( TOPN( 1, FILTER( ALL( 'Table' ), 'Table'[Code] = currentCode && 'Table'[Date] < currentDate ), 'Table'[Date] ), "@val", 'Table'[Value] ) RETURN IF( ISNUMBER( currentValue ) && ISNUMBER( prevValue ), currentValue - prevValue ) )You will need to make sure that the Value column in your main table is numeric, not text.
- AnonymousNot applicable
Hi Syndicate_Admin ,
Please refer this formula:
cons water = var last_date = CALCULATE(MAX('Table'[Date]),FILTER('Table','Table'[Date]<EARLIER('Table'[Date])&&'Table'[Code]=2235)) var last_value = CALCULATE(MAX('Table'[Value]),FILTER('Table','Table'[Date]=last_date&&'Table'[Code]=2235)) return IF(ISBLANK(last_value),BLANK(),IF('Table'[Code]=2235,'Table'[Value]-last_value))For [Cons Elect] and [Cons gas], just need to replace the 'Table'[Code] in the formula.
Result:
Best Regards,
Jay