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
JGRaiol
Helper I
Helper I

Column calculation - determining change in values

Hello all! 

 

I'd like to address a certain issue I am facing w/ Power BI.

 

I have a table in the following format (date format dd/mm/yyyy):

datevalue1value2value3
24/04/2020416
23/04/2020345

 

Values 1, 2 and 3 are parameters related to my industry.

 

I need a new column, named "analysis", that shall perform the following calculation:

 

analysis (today) = (value1(yesterday) + value2(yesterday)) - (value1(today) + value2(today)) + (value3(today) - value3(yesterday))

 

In words, the new column shall be calculated as the change in the sum of values 1 and 2, plus the addition occured on value 3.

 

In my example, the "analysis" result for "24/04/2020":

 

analysis_24/04/2020 = (3+4) - (4+1) + (6-5) = 7 - 5 + 1 = 3

 

Any insights? I've tried using FILTER combined with SELECTCOLUMNS, but considering a have a real table with far more than 2 rows, this is not working.

 

Thanks in advance!

1 ACCEPTED SOLUTION

Hi @JGRaiol ,

 

Try this calculated column:

 

Column =
VAR _date = 'Table'[date]
VAR _lastDate = CALCULATE(MAX('Table'[date]); FILTER(ALL('Table'); 'Table'[date] < _date))
VAR _tb = FILTER(ALL('Table'); 'Table'[date] = _lastDate)
RETURN
IF(COUNTROWS(_tb) > 0;
(SELECTCOLUMNS(_tb; "V1"; 'Table'[value1]) + SELECTCOLUMNS(_tb; "V2"; 'Table'[value2])) -
('Table'[value1] + 'Table'[value2]) +
('Table'[value3] - SELECTCOLUMNS(_tb; "V3"; 'Table'[value3]));
BLANK())
 
Ricardo


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!



View solution in original post

6 REPLIES 6
Ashish_Mathur
Super User
Super User

Hi,

I added a row for 25 April 2020 with the following information 6,3,4.  Here is the result i got.  You may download my PBI file from here.

Untitled.png

 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
DataZoe
Employee
Employee

This measure should work @JGRaiol :

 

Analysis = 
sumx(values(analysistable[Date]),
(calculate(sum(analysistable[v1]),PREVIOUSDAY(analysistable[Date]))
    +calculate(sum(analysistable[v2]),PREVIOUSDAY(analysistable[Date])))
-
(sum(analysistable[v1])
    +sum(analysistable[v2]))
+
(sum(analysistable[v3])
    -calculate(sum(analysistable[v3]),PREVIOUSDAY(analysistable[Date]))))

Respectfully,
Zoe Douglas (DataZoe)



Follow me on LinkedIn at https://www.linkedin.com/in/zoedouglas-data
See my reports and blog at https://www.datazoepowerbi.com/

az38
Community Champion
Community Champion

Hi @JGRaiol 

it could be a column like

Column = 
var _thisDate = Table[Date]
var _prevDate = CALCULATE(MAX(Table[Date]), Table[Date] < _thisDate)
var _prevVal1 = CALCULATE(MAX(Table[Value1]), Table[Date] = _prevDate)
var _prevVal2 = CALCULATE(MAX(Table[Value2]), Table[Date] = _prevDate)
var _prevVal3 = CALCULATE(MAX(Table[Value3]), Table[Date] = _prevDate)
RETURN
_prevVal1 + _prevVal2 - ([Value1] + [Value2]) + ([Value3] - _prevVal3 )

do not hesitate to give a kudo to useful posts and mark solutions as solution
LinkedIn

Hello @az38, thank you so much for your reply!

 

I perfectly understood your implementation, but when I put it on my BI, all prevdate and prevVals are returning blank, and not the proper value. I am implementing this as a calculated column.

 

Any insights?

 

Many thanks!

Hi @JGRaiol ,

 

Try this calculated column:

 

Column =
VAR _date = 'Table'[date]
VAR _lastDate = CALCULATE(MAX('Table'[date]); FILTER(ALL('Table'); 'Table'[date] < _date))
VAR _tb = FILTER(ALL('Table'); 'Table'[date] = _lastDate)
RETURN
IF(COUNTROWS(_tb) > 0;
(SELECTCOLUMNS(_tb; "V1"; 'Table'[value1]) + SELECTCOLUMNS(_tb; "V2"; 'Table'[value2])) -
('Table'[value1] + 'Table'[value2]) +
('Table'[value3] - SELECTCOLUMNS(_tb; "V3"; 'Table'[value3]));
BLANK())
 
Ricardo


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!



Thank you! That worked!

 

Best regards

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.