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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anonymous
Not applicable

Delta Calculation strange results

Hi there,

 

I have two tables in my SQL Server 2017 The CurrentMemberYr produce the expected results but

The CurrentMemberBr with the slicer did not as you can see in the attachment

How do I fix this? Do I need to put middle table like many to many?

 

Here is the code in TSQL for PowerBI (two tables)

create table CurrentMemberYr
(
Years int null,
CurrentMembers int null
)
GO
Insert into CurrentMemberYr(Years,CurrentMembers)
Values(2015,200),(2016,300),(2017,550),(2018,400)

--CurrentMemberYr in PowerBI
With myCTE
AS
(
Select Years,
CurrentMembers
from CurrentMemberYr
)
Select Years,
CurrentMembers,
Delta = CurrentMembers - Lag(CurrentMembers) Over (order by Years)
From myCTE

create table CurrentMemberBr
(
Years int null,
Branch Varchar(10) null,
CurrentMembers int null
)
Insert into CurrentMemberBr(Years,Branch,CurrentMembers) Values
(2015,'A',200),(2016,'A',300),(2017,'A',550),(2018,'A',400),
(2015,'B',200),(2016,'B',300),(2017,'B',550),(2018,'B',400),Current
(2015,'C',200),(2016,'C',300),(2017,'C',550),(2018,'C',400),
(2015,'D',200),(2016,'D',300),(2017,'D',550),(2018,'D',400)

--CurrentMemberBr in PowerBI
;With myCTE
AS
(
Select Years,
Branch,
CurrentMembers
from CurrentMemberBr
)
Select Years,
Branch,
CurrentMembers,
Delta = CurrentMembers - Lag(CurrentMembers) Over (order by Years)
From myCTE

 Delta Calculation.jpg

1 ACCEPTED SOLUTION
DanielV91
Frequent Visitor

Hello @Anonymous

 

Can you please try the following query and let me know if it works?

 

SELECT 
Branch,
Years,
CurrentMembers, CASE
WHEN Branch != LAG(Branch, 1) OVER (ORDER BY Branch, Years) THEN NULL ELSE CurrentMembers - LAG(CurrentMembers,1) OVER (ORDER BY Branch, Years)
END AS Delta FROM CurrentMemberBr ORDER BY Branch, Years

 

View solution in original post

4 REPLIES 4
DanielV91
Frequent Visitor

Hello @Anonymous

 

Can you please try the following query and let me know if it works?

 

SELECT 
Branch,
Years,
CurrentMembers, CASE
WHEN Branch != LAG(Branch, 1) OVER (ORDER BY Branch, Years) THEN NULL ELSE CurrentMembers - LAG(CurrentMembers,1) OVER (ORDER BY Branch, Years)
END AS Delta FROM CurrentMemberBr ORDER BY Branch, Years

 

Anonymous
Not applicable

Daniel,

 

Yes it works

 

Thank you so much

 

Oded Dror

quentin_vigne
Solution Sage
Solution Sage

Hi @Anonymous

 

Did you add a relationship between your two tables in the relationship tab ? 

If not you should probably add a new one on an PK value.

 

Also check if it is a bidirectionnal filter between them 

 

- Quentin

Anonymous
Not applicable

Quentin,

 

No, there is no connection between them. Table CurrentMemberYr is to show what the currect value looks like (expected results)

Table CurrentMemberBr show the error, I think I need to add dynamic parameter and convert the code to a stored proc

 

Thanks,

Ed Dror

 

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.