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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
peterschuller
Frequent Visitor

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. 

 

1 ACCEPTED SOLUTION

@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

   

View solution in original post

8 REPLIES 8
AlB
Super User
Super User

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

  

@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

 

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: PartIDStockHistoryTable: PartIDStockHistory

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

 

Table: TableResTable: TableRes

 
I used this DAX formula:
 
Query.JPG
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.
 
 
 
 

 

@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

Thanks again for all your help and quick replies @AlB ,

 

I can see that it is working in your pbix. 

Unfortunately it still not working in my file.

My original file is called StockImport and has 505.167 rows.

Table StockImportTable StockImport

I created a new table called StockHistory using the following DAX formula:

Query.JPG

The result is a new table Stockhistory which is exactly the same as the StockImport file and contains 505.167 rows.

 

Any idea what goes wrong?

 

 

@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

   

Hi @AlB ,

Thanks for sharing. 

I have a similar issue with the script applied not working, I also have no active relationships on the table. 

Is it possible that the script does not work when e.g. pulling from a query? 

Hi @miggy46 

Right-click on the relationship icon (or line) and choose Properties.  In the window that pops up there will be a checkbox around  the bottom to toggle the relationship active/inactive

 

AlB_0-1694423635841.png

 

SU18_powerbi_badge

Please accept the solution when done and consider giving a thumbs up if posts are helpful. 

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

 

 

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors