且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 Power BI/Power Query 中添加缺失的日期行并取上面行的值

更新时间:2023-10-22 10:15:46

您可以在 DAX 中通过创建一个包含您范围内所有日期的新表来执行此操作,如下所示:

You can do this in DAX by creating a new table with all of the dates in your range as follows:

FullTable = 
ADDCOLUMNS(
    CALENDAR(MIN(Table1[Date]), MAX(Table1[Date])),
    "Quantity",
    LOOKUPVALUE(
        Table1[Quantity],
        Table1[Date],
        MAXX(
            FILTER(Table1, Table1[Date] <= EARLIER([Date])),
            [Date]
        )
    )
)

CALENDAR 函数为您提供原始表中从最小日期到最大日期的日期范围.从那里,我们添加一个新列,Quantity,并将其定义为我们在原始表中查找最大日期的 Quantity 时获得的值发生在当前行中的日期或之前.

The CALENDAR function gives you a range of dates from you minimum to maximum dates in your original table. From there, we add a new column, Quantity, and define it as the value we get when looking up the Quantity in the original table for the date that was the maximum date occurring on or before the date in the current row.