1

I'm tying to compare Azure Cost Management cost to the previous month. The goal is to understand what resources reduce their costs.

I followed this guide that helped me setup PREVIOUSMONTH this way:

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?

EDIT: I'm adding new raw data:

Here is the problem:

  • the database "preview" exists in October and November but not in December.
  • the database "dev" exists only in December

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

enter image description here

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.

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.

Francesco Mantovani
  • 10,216
  • 13
  • 73
  • 113
  • You are using a wrong field for the PREVIOUSMONTH function. It expects continuous dates from a proper calendar table, not from a transactional fact table. – RADO Jan 25 '23 at 23:04
  • Thank you @RADO but I don't think this is the problem. How can I check your scenario? How can I create a calendar? – Francesco Mantovani Jan 26 '23 at 07:44
  • This is exactly the problem. I downloaded you pbix and built a solution. See the answer below. – RADO Jan 26 '23 at 10:48

2 Answers2

2

What you want to do is to modify the current context (row context); We can do this by using the 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:

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

enter image description here

msta42a
  • 3,601
  • 1
  • 4
  • 14
  • Nice demonstration of window function! – Marcus Jan 20 '23 at 11:39
  • The first was returning a blank column. The second is working. However there are missing rows. This is probably due to the fact that I'm targeting December. Some resources were deleted in November and because the calculation is based on December and these resources are not there, they are not reported in the dashboard – Francesco Mantovani Jan 20 '23 at 13:42
  • Maybe I should calculate November --> to the next month? – Francesco Mantovani Jan 20 '23 at 13:42
  • Edit your question and add some dummy data in tabular form (text rather than screenshot; It will be easier. – msta42a Jan 20 '23 at 13:50
  • Thank you @msta42a, I added a file with the raw data – Francesco Mantovani Jan 20 '23 at 16:17
  • I wasn't very precise. I meant the source data you're working on. – msta42a Jan 20 '23 at 20:05
  • The source is this: https://learn.microsoft.com/en-us/power-bi/connect-data/desktop-connect-azure-cost-management Now I cannot give you the whole data because I need to anonymize everything. There are too many resources in 2 months of Azure Cost Management... Can you suggest a way to anonymize that? – Francesco Mantovani Jan 21 '23 at 21:11
  • Hi @msta42a , I added the Power BI file + the Excel file and a picture. Do you see the problem now? – Francesco Mantovani Jan 24 '23 at 17:03
2

You have 2 problems:

  1. Data model is missing a calendar table
  2. Function "PREVIOUSMONTH" is using incorrect field

To fix it, you need to add a proper calendar table to your model, and then use it for PREVIOUSMONTH. I quickly prototyped it for you to prove:

enter image description here

Your data model should look like this:

enter image description here

You can create the calendar table in many ways - as a calculated table in DAX, in PowerQuery, or import from a database or file. I always prefer to import.

For quick prototyping, you can create a calculated table using DAX code like this:

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):

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.

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. If you don't build a correct model, you DAX will be complicated and often give wrong results and poor performance.

RADO
  • 7,733
  • 3
  • 19
  • 33
  • Much worst dear @RADO , not sure why but the simple `PreviousMonth = CALCULATE(SUM('Usage details'[costInBillingCurrency]), PREVIOUSMONTH('Usage details'[date].[Date]))` worked perfectly without the need of a calendar table. I must have had a brain fart when I test it the first time. – Francesco Mantovani Jan 26 '23 at 11:54
  • 1
    You shouldn't use time intelligence functions without a date table. You may get incorrect results which are apparent at first... – Davide Bacci Jan 27 '23 at 10:00