Forum Discussion

DBrito79's avatar
DBrito79
Frequent Visitor
2 years ago
Solved

Help making an inventory calendar table in Power query Editor in M

Hello. I have a huge table "SOWS" wich each line represents one animal. In each line I have columns with the ID of the animal (unique ID), a "EntryDate" of the animal in the farm and a "OutDate" wi...
  • jgeddes's avatar
    2 years ago

    You can try adding this query. You will need to adjust the start date of the 'List.Dates' step (currently Dec 1, 2023) along with the number of days you want in the table (currently 180).

    let
        Source = 
        List.Dates(
            #date(2023,12,01), 
            180, 
            #duration(1,0,0,0)
        ),
        convertDatesToTable = 
        Table.FromList(
            Source, 
            Splitter.SplitByNothing(), 
            null, 
            null, 
            ExtraValues.Error
        ),
        changeToDateType = 
        Table.TransformColumnTypes(
            convertDatesToTable,
            {{"Column1", type date}}
        ),
        renameColumn = 
        Table.RenameColumns(
            changeToDateType,
            {{"Column1", "Date"}}
        ),
        addDailyEntries = 
        Table.AddColumn(
            renameColumn, 
            "dailyEntries", 
            (x)=>List.Count(Table.SelectRows(SOWS, each [EntryDate] = x[Date])[ID]
            ), 
            Int64.Type
        ),
        addDailyOuts = 
        Table.AddColumn(
            addDailyEntries, 
            "dailyOuts", 
            (x)=>List.Count(Table.SelectRows(SOWS, each [OutDate] = x[Date])[ID]), 
            Int64.Type
        ),
        addCumulativeIns = 
        Table.AddColumn(
            addDailyOuts, 
            "cumulativeIns", 
            (x)=>List.Count(Table.SelectRows(SOWS, each [EntryDate] <= x[Date])[ID]), 
            Int64.Type
        ),
        addCumulativeOuts = 
        Table.AddColumn(
            addCumulativeIns, 
            "cumulativeOuts", 
            (x)=>List.Count(Table.SelectRows(SOWS, each [OutDate] <= x[Date])[ID]), 
            Int64.Type
        ),
        addInventory = 
        Table.AddColumn(
            addCumulativeOuts, 
            "Inventory", 
            each [cumulativeIns] - [cumulativeOuts], 
            Int64.Type
        )
    in
        addInventory