简体   繁体   English

PREVIOUSMONTH 返回空行或错误数据

[英]PREVIOUSMONTH returns blank rows or wrong data

I'm tying to compare Azure Cost Management cost to the previous month.我想将 Azure 成本管理成本与上个月进行比较。 The goal is to understand what resources reduce their costs.目标是了解哪些资源可以降低成本。

I followed this guide that helped me setup PREVIOUSMONTH this way:我按照本指南帮助我以这种方式设置了 PREVIOUSMONTH:

PreviousMonth = CALCULATE(
    SUM('Usage details'[costInBillingCurrency]), 
    PREVIOUSMONTH('Usage details'[date].[Date]))

But this formula only returns a blank column.但是这个公式只返回一个空白列。

So I followed this guide that helped me setup this code:所以我遵循了帮助我设置此代码的指南

PreviousMonth = CALCULATE(
    SUM('Usage details'[costInBillingCurrency]), 
    PREVIOUSMONTH('Usage details'[date].[Date]),
    ALLEXCEPT('Usage details','Usage details'[subscriptionName],'Usage details'[resourceGroupName],'Usage details'[ResourceName]  ))

Now values are returned but they are wrong.现在返回了值,但它们是错误的。

So I setup this measure and again the column is empty:所以我设置了这个度量,但该列再次为空:

Measure = CALCULATE(
    SUM('Usage details'[costInBillingCurrency]),
    MONTH('Usage details'[date])=MONTH(TODAY())-1,
    YEAR('Usage details'[date])=YEAR(TODAY()))

So how to compare the Azure cost of rescues December VS November?那么如何比较 Azure 的救援成本 12 月 VS 11 月?

EDIT: I'm adding new raw data:编辑:我正在添加新的原始数据:

Here is the problem:这是问题所在:

  • the database "preview" exists in October and November but not in December.数据库“预览”在 10 月和 11 月存在,但在 12 月不存在。
  • the database "dev" exists only in December数据库“dev”仅在 12 月存在

This means that if I select December as a current month I should see dev for the current month but not for the previous month这意味着如果我将 select 12 月作为当前月份,我应该会看到当前月份的开发,但不会看到上个月的开发

在此处输入图像描述

And in the other hand I should see the preview database for the month of November but an empty space for the month of December.另一方面,我应该看到 11 月的预览数据库,但 12 月的空间是空白的。

Ideally I would like to use the color Red/Green for the current month and color in green if the costs is decreased, red if the cost has increased.理想情况下,我希望当月使用红色/绿色,如果成本降低则使用绿色,如果成本增加则使用红色。

What you want to do is to modify the current context (row context);你要做的是修改当前上下文(行上下文); We can do this by using the function ALL();我们可以使用 function ALL();

CostPrevMonth = 
var _currentdate = SELECTEDVALUE('Table'[date])
return
CALCULATE(sum('Table'[Cost]), FILTER(ALL('Table'[date]), 'Table'[date] = DATE(YEAR(_currentdate),MONTH(_currentdate)-1,1)))

We can also use one of the new window function in DAX:我们还可以在 DAX 中使用新的 window function 之一:

CostPrveMonthOffset = CALCULATE(SUM('Table'[Cost]), OFFSET(-1, , ORDERBY('Table'[date])))

在此处输入图像描述

You have 2 problems:你有两个问题:

  1. Data model is missing a calendar table数据model缺少日历表
  2. Function "PREVIOUSMONTH" is using incorrect field Function “PREVIOUSMONTH”使用的字段不正确

To fix it, you need to add a proper calendar table to your model, and then use it for PREVIOUSMONTH.要修复它,您需要将适当的日历表添加到您的 model,然后将其用于 PREVIOUSMONTH。 I quickly prototyped it for you to prove:我很快制作了原型供您证明:

在此处输入图像描述

Your data model should look like this:您的数据 model 应如下所示:

在此处输入图像描述

You can create the calendar table in many ways - as a calculated table in DAX, in PowerQuery, or import from a database or file.您可以通过多种方式创建日历表 - 作为 DAX 中的计算表、PowerQuery 中的计算表,或者从数据库或文件中导入。 I always prefer to import.我总是喜欢进口。

For quick prototyping, you can create a calculated table using DAX code like this:为了快速制作原型,您可以使用 DAX 代码创建一个计算表,如下所示:

Date = 
VAR MinYear = YEAR ( MIN ( Data[Date] ) )
VAR MaxYear = YEAR ( MAX ( Data[Date] ) )
RETURN
ADDCOLUMNS (
    FILTER (
        CALENDARAUTO( ),
        AND ( YEAR ( [Date] ) >= MinYear, YEAR ( [Date] ) <= MaxYear )
    ),
    "Year", "CY " & YEAR ( [Date] ),
    "Year-Month", FORMAT ( [Date], "yyyy-mm" )
)

I added just a couple of fields (year and year-month), but you should create all kinds of fields that are useful for your reports.我只添加了几个字段(年和年-月),但您应该创建对报告有用的各种字段。

Then, connect this new table to table "Data" using date fields.然后,使用日期字段将这个新表连接到表“数据”。 Next, create 2 measures (change names as you please):接下来,创建 2 个度量(根据需要更改名称):

Total Cost = SUM(Data[costInBillingCurrency])

and

Previous Month Cost = CALCULATE( [Total Cost], PREVIOUSMONTH( 'Date'[Date]))

Notice that I used date from the calendar table, not DATA table.请注意,我使用的是日历表中的日期,而不是 DATA 表中的日期。

Finally, I added "Year-Month" field from the calendar table (not from "Data" table) to your visual, to show that it works.最后,我将日历表(而不是“数据”表)中的“年月”字段添加到您的视觉对象中,以表明它有效。 If you want to use a slicer, also use date fields from the calendar table.如果要使用切片器,还可以使用日历表中的日期字段。

Power BI is designed to report from a proper dimensional model ( a star schema), with dimensions and fact tables. Power BI 旨在从适当的维度 model(星型模式)生成报告,其中包含维度和事实表。 If you don't build a correct model, you DAX will be complicated and often give wrong results and poor performance.如果你没有构建正确的 model,你的 DAX 将会很复杂并且经常给出错误的结果和糟糕的性能。

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

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