Forum Discussion

santoshlearner2's avatar
santoshlearner2
Resolver II
2 years ago
Solved

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:

 

 

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

 

  • 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

     

7 Replies

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

    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!

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

     

     

      • lbendlin's avatar
        lbendlin
        Super User

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

  • PowerBIDave's avatar
    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

     

    • santoshlearner2's avatar
      santoshlearner2
      Resolver II

      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

      • PowerBIDave's avatar
        PowerBIDave
        Regular Visitor

        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.