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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
santoshlearner2
Resolver I
Resolver I

Daily Changes from the available dates

Dear All,

 

I have daily stock company / product / stock quantity and price /on  a daily basis the value subtracts the value from the previous day by a calculated column, instead of a measure, the issue is when there the date is not available it does not give the daily change, for eg if the available dates are

 

29th May

1st June

2nd June

5th June

 

Then on 1st June since 31st may date is not available in the table it does not give the value, similarly on 5th June, since 4th June is not available it does not give the value.

 

wanted to compute daily stock changes and price changes, the highlighted cells refers to the respective Plus / Minus done.

 

Screenshot:

 

santoshlearner2_0-1724676021275.png

 

Data of the above : 

 

DateCompanyProductStockPriceStock ChangePrice Change
05-06-2024ABCBook10010  
05-06-2024MicrosoftBook5020  
05-06-2024FordBook6030  
05-06-2024ABCPen7040  
05-06-2024MicrosoftPen7450  
05-06-2024FordPen6060  
05-06-2024ABCEraser4270  
05-06-2024MicrosoftEraser3080  
05-06-2024FordEraser4290  
       
03-06-2024ABCBook1021525
03-06-2024MicrosoftBook532535
03-06-2024FordBook643545
03-06-2024ABCPen7465425
03-06-2024MicrosoftPen78204-30
03-06-2024FordPen64304-30
03-06-2024ABCEraser467040
03-06-2024MicrosoftEraser34304-50
03-06-2024FordEraser46100410
       
28-05-2024ABCBook104111296
28-05-2024MicrosoftBook564563431
28-05-2024FordBook682564221
28-05-2024ABCPen78146481
28-05-2024MicrosoftPen823694349
28-05-2024FordPen681364106
28-05-2024ABCEraser50136466
28-05-2024MicrosoftEraser38136941339
28-05-2024FordEraser50133641236
       

 

How to do it .

 

Thanks in advance for your inputs.


Thank you


Santosh

 

1 ACCEPTED SOLUTION
PowerBIDave
Regular Visitor

@santoshlearner2

Assuming your existing table just has columns named Date, Company, Product, Stock, and Price, you could create two calculated columns with the code below to produce the result you want. It may not be the most elegant code but it works. There are potentially better ways of doing this by modelling the data correctly and calculating the values with measures, but if you have only the one table of data and insist on using calculated columns then this should work. Good luck.

 

************ First Calculated Column ************

Stock Change =
VAR __currentDate = StockTable[Date]
VAR __currentCompany = StockTable[Company]
VAR __currentProduct = StockTable[Product]
VAR __currentStockValue = StockTable[Stock]
VAR __filteredTable =
FILTER(
StockTable,
StockTable[Company] = __currentCompany && StockTable[Product] = __currentProduct && StockTable[Date] > __currentDate
)
VAR __nextDate =
MINX(
__filteredTable,
[Date]
)
VAR __result =
IF(
NOT ISBLANK(__nextDate),
__currentStockValue -
MINX(
FILTER(
__filteredTable,
[Date] = __nextDate
),
[Stock]
)
)
RETURN
__result

 

************ Second Calculated Column ************

Price Change =
VAR __currentDate = StockTable[Date]
VAR __currentCompany = StockTable[Company]
VAR __currentProduct = StockTable[Product]
VAR __currentPriceValue = StockTable[Price]
VAR __filteredTable =
FILTER(
StockTable,
StockTable[Company] = __currentCompany && StockTable[Product] = __currentProduct && StockTable[Date] > __currentDate
)
VAR __nextDate =
MINX(
__filteredTable,
[Date]
)
VAR __result =
IF(
NOT ISBLANK(__nextDate),
__currentPriceValue -
MINX(
FILTER(
__filteredTable,
[Date] = __nextDate
),
[Price]
)
)
RETURN
__result

 

View solution in original post

7 REPLIES 7
PowerBIDave
Regular Visitor

@santoshlearner2

Assuming your existing table just has columns named Date, Company, Product, Stock, and Price, you could create two calculated columns with the code below to produce the result you want. It may not be the most elegant code but it works. There are potentially better ways of doing this by modelling the data correctly and calculating the values with measures, but if you have only the one table of data and insist on using calculated columns then this should work. Good luck.

 

************ First Calculated Column ************

Stock Change =
VAR __currentDate = StockTable[Date]
VAR __currentCompany = StockTable[Company]
VAR __currentProduct = StockTable[Product]
VAR __currentStockValue = StockTable[Stock]
VAR __filteredTable =
FILTER(
StockTable,
StockTable[Company] = __currentCompany && StockTable[Product] = __currentProduct && StockTable[Date] > __currentDate
)
VAR __nextDate =
MINX(
__filteredTable,
[Date]
)
VAR __result =
IF(
NOT ISBLANK(__nextDate),
__currentStockValue -
MINX(
FILTER(
__filteredTable,
[Date] = __nextDate
),
[Stock]
)
)
RETURN
__result

 

************ Second Calculated Column ************

Price Change =
VAR __currentDate = StockTable[Date]
VAR __currentCompany = StockTable[Company]
VAR __currentProduct = StockTable[Product]
VAR __currentPriceValue = StockTable[Price]
VAR __filteredTable =
FILTER(
StockTable,
StockTable[Company] = __currentCompany && StockTable[Product] = __currentProduct && StockTable[Date] > __currentDate
)
VAR __nextDate =
MINX(
__filteredTable,
[Date]
)
VAR __result =
IF(
NOT ISBLANK(__nextDate),
__currentPriceValue -
MINX(
FILTER(
__filteredTable,
[Date] = __nextDate
),
[Price]
)
)
RETURN
__result

 

Hi,

 

Superb of you, i am getting the values, but the issue is say if i am on 25th June , the value is 25.105 and 24th june the june the value is 25.000, then on 25th june i should get 0.105, but the value of 0.105 is showing for 24th june, it is not showing next to the 25th june date.  I think the previous day measure should be changed, can you help

@santoshlearner2

You just need to tweak the code I provided previously to get the changes going forward.

Something along the lines of the following.


************ First Calculated Column ************

Stock Change Going Forward =
VAR __currentDate = StockTable[Date]
VAR __currentCompany = StockTable[Company]
VAR __currentProduct = StockTable[Product]
VAR __currentStockValue = StockTable[Stock]
VAR __filteredTable =
FILTER(
StockTable,
StockTable[Company] = __currentCompany && StockTable[Product] = __currentProduct && StockTable[Date] < __currentDate
)
VAR __prevDate =
MAXX(
__filteredTable,
[Date]
)
VAR __result =
IF(
NOT ISBLANK(__prevDate),
__currentStockValue -
MINX(
FILTER(
__filteredTable,
[Date] = __prevDate
),
[Stock]
)
)
RETURN
__result


************ Second Calculated Column ************

Price Change Going Forward =
VAR __currentDate = StockTable[Date]
VAR __currentCompany = StockTable[Company]
VAR __currentProduct = StockTable[Product]
VAR __currentPriceValue = StockTable[Price]
VAR __filteredTable =
FILTER(
StockTable,
StockTable[Company] = __currentCompany && StockTable[Product] = __currentProduct && StockTable[Date] < __currentDate
)
VAR __prevDate =
MAXX(
__filteredTable,
[Date]
)
VAR __result =
IF(
NOT ISBLANK(__prevDate),
__currentPriceValue -
MINX(
FILTER(
__filteredTable,
[Date] = __prevDate
),
[Price]
)
)
RETURN
__result

************************************

 

Hope that helps.

 

If this answers your question, please mark as a solution so others can find.

 

lbendlin
Super User
Super User

Do you need the results as a table or could it be a waterfall or ribbon chart?

 

lbendlin_0-1724680465096.png

 

Hi,

 

I wanted to put the same in a matrix showing as a trend, 

A matrix visual is not good for showing trends. A graph is easier to comprehend.

Mahesh0016
Super User
Super User

@santoshlearner2  Right-click on the visual field section and select "Show Items with No Data."( As mention in below screenshot).

Mahesh0016_0-1724680335112.png

Alternatively, create a date table and use it to plot the date column in a table visual. This will ensure you see continuous data.

@santoshlearner2  I hope this helps. Thank You!

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.