简体   繁体   English

Power BI:使用不相关的日期表创建度量

[英]Power BI: Use unrelated date table to create a measure

Hi I have been struggling with this for a bit now and I hope I didn't miss a previous question.嗨,我已经为此苦苦挣扎了一段时间,希望我没有错过以前的问题。 I am trying to get a count of the vehicles we have based on an EOMONTH date.我正在尝试根据 EOMONTH 日期计算我们拥有的车辆数量。 We are buying and selling cars on a regular basis and for reporting we need to know how many we had at the end of each month and the report is a rolling 12 months.我们定期买卖汽车,为了报告,我们需要知道每个月底有多少辆汽车,报告是滚动的 12 个月。

I've tried creating the relationship with the purchasedate of the vehicle to the date of my date table but when I create the measure (Used to calculate the number of vehicles purchased but haven't been sold):我尝试创建与日期表日期的车辆购买日期的关系,但是当我创建度量时(用于计算已购买但尚未售出的车辆数量):

SalesBlank = CALCULATE( COUNT(Vehicles[MVANumber]), FILTER(Vehicles, Vehicles[purchasedate] <= RELATED('Date'[EOMONTH]) && ISBLANK(Vehicles[saledate]))) SalesBlank = CALCULATE(COUNT(Vehicles[MVANumber]), FILTER(Vehicles, Vehicles[purchasedate] <= RELATED('Date'[EOMONTH]) && ISBLANK(Vehicles[saledate])))

I only get a count of vehicles purchased that month and don't have a sale date - I'm not surprised because my relationship with the date table is the purchase date.我只计算了当月购买的车辆数量并且没有销售日期——我并不感到惊讶,因为我与日期表的关系就是购买日期。

How can I set up a measure to look at the date table and filter the vehicles table with this logic: purchasedate <= date[EOMONTH] && ISBLANK(salesdate)如何设置度量来查看日期表并使用以下逻辑过滤车辆表:购买 <= date[EOMONTH] && ISBLANK(salesdate)

Any help would be greatly appreciated,!任何帮助将不胜感激,! Thanks, Matt谢谢,马特

Sample Data and Desired Results样本数据和所需结果

Relationships关系

If I understand you correctly, you want to get a count of the vehicles on hand at the end of each month.如果我理解正确的话,你想在每个月底对手头的车辆进行计数。 That could be calculated by counting the vehicles with a purchase date less than or equal to the selected end of month and subtracting the count of vehicles with a sale date less than or equal to the selected end of month.这可以通过计算购买日期小于或等于所选月末的车辆数并减去销售日期小于或等于所选月末的车辆数来计算。

You can create an active relationship between Vehicle[PurchaseDate] and Date[Date].您可以在 Vehicle[PurchaseDate] 和 Date[Date] 之间创建活动关系。 Then create an inactive relationship based upon Vehicles[SaleDate] and Date[Date].然后根据 Vehicles[SaleDate] 和 Date[Date] 创建一个非活动关系。

You could use a measure that is something like this:您可以使用类似这样的度量:

Inventory Count =
VAR MaxDate =
    MAX ( 'Date'[Date] )
VAR MinDate =
    CALCULATE ( MIN ( 'Date'[Date] ), ALL ( 'Date' ) )
VAR Purch =
    CALCULATE (
        COUNT ( 'Vehicles'[VehicleID] ),
        DATESBETWEEN ( 'Date'[Date], MinDate, MaxDate )
    )
VAR Sales =
    CALCULATE (
        COUNT ( 'Vehicles'[VehicleID] ),
        USERELATIONSHIP ( 'Date'[Date], Vehicles[Sale Date] ),
        DATESBETWEEN ( 'Date'[Date], MinDate, MaxDate )
    )
VAR MaxPurDt =
    CALCULATE ( MAX ( 'Vehicles'[Purchase Date] ), ALL ( 'Vehicles' ) )
VAR MaxSlDt =
    CALCULATE ( MAX ( 'Vehicles'[Sale Date] ), ALL ( 'Vehicles' ) )
RETURN
    IF (
        MIN ( 'Date'[Date] ) <= MaxPurDt
            || MIN ( 'Date'[Date] ) <= MaxSlDt,
        Purch - Sales
    )

This measure gets a cumulative count of purchases and a cumulative count of sales and then subtracts them.此度量获取购买的累积计数和销售的累积计数,然后将它们减去。 The IF check is to avoid propagation of cumulative totals beyond the maximum date in the Vehicle table. IF 检查是为了避免累积总计传播超出 Vehicle 表中的最大日期。

I'm not sure how to interpret your showing of just 3 months in the desired results.我不确定如何解释您在预期结果中仅显示 3 个月。 This will produce the same answers as what you have, but without a filter applied to the table, it starts at 3/31/2016 (the date of the first sale).这将产生与您所拥有的答案相同的答案,但没有对表格应用过滤器,它从 2016 年 3 月 31 日(首次销售日期)开始。

Edit: There's probably a more efficient way along the lines you were thinking, but it is escaping me at the moment.编辑:按照您的想法,可能有一种更有效的方法,但目前我是 escaping。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM