Forum Discussion

peterschuller's avatar
peterschuller
Frequent Visitor
6 years ago
Solved

Add values at missing dates

I have two different tables:

1) a date table

Date

01-01-19
01-02-19
01-03-19
01-04-19
01-05-19
01-06-19
01-07-19
01-08-19
01-09-19

 

2) a table with stockquantities per date, but this only contains a value if the stock has been changed. 

 

Partcode Date            StockQty

A101-01-195
A101-05-196
A101-08-194
A201-03-192
A201-08-193
A201-09-191

 

I would like to write a measure with fills the empty dates in the date table with the value from the previous non blank date in the stock data table. The result should be equal to the table below:

Partcode date StockQty

A101-01-195
A101-02-195
A101-03-195
A101-04-195
A101-05-196
A101-06-196
A101-07-196
A101-08-194
A101-09-194
A201-03-192
A201-04-192
A201-05-192
A201-06-192
A201-07-192
A201-08-193
A201-09-191

 

Can anyone help me? If tried for several hours now and am quit desperate. 

 

  • AlB's avatar
    AlB
    6 years ago

    peterschuller 

    I had a quick look at the file. There are relationships. If you delete them or make them inactive i believe it works (the resulting table has then around 7 million rows)

     

    Please mark the question solved when done and consider giving kudos if posts are helpful.

    Cheers  Datanaut

       

8 Replies

  • AlB's avatar
    AlB
    Community Champion

    Hi peterschuller 

    Try creating a new table (Table2 is the second one you show):

    TableRes =
    GENERATE (
        Table2;
        GENERATESERIES (
            CALCULATE ( DISTINCT ( Table2[Date] ) );
            VAR Current_ =
                CALCULATE ( DISTINCT ( Table2[Date] ) )
            VAR Next_ =
                CALCULATE (
                    MIN ( Table2[Date] );
                    Table2[Date] > Current_;
                    ALL ( Table2[StockQty] )
                )
            RETURN
                IF ( NOT ISBLANK ( Next_ ); Next_ - 1; Current_ )
        )
    )
    

    You'll have the actual date column in "Value". Delete the column  'Date'  and rename Value as Date.  All this would probably be more elegant in M.

     

    Please mark the question solved when done and consider giving kudos if posts are helpful.

    Cheers  Datanaut

      

    • peterschuller's avatar
      peterschuller
      Frequent Visitor

      Thanks a lot AlB,

       

      I think this is getting in the right direction, but so far the new table looks almost the same as the original table.

      I have selected one PartID and this is how it looked in the original table:

       

      Table: PartIDStockHistory

      In the newly created table the same selection looks like this:

       

      Table: TableRes

       
      I used this DAX formula:
       
      I think I miss the link with my first table (the date table) which contains all the dates of 2018.
      The result should be a table with a stockposition for each of the 365 days of 2018.
       
       
       
       

       

      • AlB's avatar
        AlB
        Community Champion

        peterschuller 

        It seems to work in the tests I ran. Have a look at the attached file, where I've also included a "cleaned" version with the updated column names deleting the old "Date" column:   

        TableResCleaned = 
        SELECTCOLUMNS (
            GENERATE (
                Table1;
                GENERATESERIES (
                    CALCULATE ( DISTINCT ( Table1[Date] ) );
                    VAR Current_ =
                        CALCULATE ( DISTINCT ( Table1[Date] ) )
                    VAR Next_ =
                        CALCULATE (
                            MIN ( Table1[Date] );
                            Table1[Date] > Current_;
                            ALL ( Table1[StockQty] )
                        )
                    RETURN
                        IF ( NOT ISBLANK ( Next_ ); Next_ - 1; Current_ )
                )
            );
            "Partcode"; [Partcode];
            "Date"; [Value];
            "StockQty"; [StockQty]
        )

         

        Please mark the question solved when done and consider giving kudos if posts are helpful.

        Cheers  Datanaut

    • Anonymous's avatar
      Anonymous
      Not applicable

      AlB 

      For personal enrichment, could you clarify how this piece of code is working? In the sample file that you share the above code works as intended, but when I tried to incorporate it into my own PBI, sometimes it's not generating a date

      e.g. in the table below - for unclear reasons - 2023-04-26 was not generated

      DatePresent in original tableGenerated by your code
      2023-04-18TrueTrue
      2023-04-19TrueTrue
      2023-04-20FalseTrue
      2023-04-21FalseTrue
      2023-04-22TrueTrue
      2023-04-23TrueTrue
      2023-04-24TrueTrue
      2023-04-25TrueTrue
      2023-04-26FalseFalse
      2023-04-27TrueTrue
      2023-04-28TrueTrue
      2023-04-29TrueTrue