Rolling average a certain period using a DAX measure in Power BI - postgresql

I tried generating a DAX measure as a rolling average. I don't quite know how to insert the measure I need rolling averaged as "CPI_annualized". It is not giving an option to bring CPI_annualized into the Rolling Average calc.
This is the error I am given when trying to construct the RollingAverage VAR in the measure P_MA
The syntax for 'CALCULATE' is incorrect. DAX(VAR __LAST_DATE = LASTDATE('public econometrics'[date]) ......
This is the DAX measure that I am trying to complete :
P_MA =
VAR __NUM_PERIODS = 3
VAR __LAST_DATE = LASTDATE('public econometrics'[date])
VAR RollingAverage =
AVERAGEX(
DATESBETWEEN(
'public econometrics'[date],
DATEADD(__LAST_DATE, -__NUM_PERIODS, MONTH),
__LAST_DATE)
CALCULATE([CPILFESL]))
)
RETURN RollingAverage
This is the DAX measure that I am trying to use in the rolling calculation with the data in my dB given monthly. This works as below.
CPI_annualized = (CALCULATE(SUM('public econometrics'[value]),'public
econometrics'[econometric_name]=="CPILFESL")/CALCULATE(SUM('public
econometrics'[value]),'public
econometrics'[econometric_name]=="CPILFESL",SAMEPERIODLASTYEAR('public econometrics'[date])))-1
Inserting this measure in a line chart gives my this table.
date
CPILFESL
1 January, 1978
6.41%
1 February, 1978
6.20%

I think you are getting error message due to this line:
CALCULATE([CPILFESL])
In Dax Calculate measure, you cannot directly calculate a value, instead you have to include a number calculation function such as sum, max, min, here is the example:
CALCULATE(sum([CPILFESL]))
In addition, you can get rid of Calculate measure when there is no filter expression, such as sum([CPILFESL]) is enough.
A proper way of using calculate is to filter the value, as shown in online documentation
CALCULATE(
SUM(Sales[Sales Amount]),
'Product'[Color] = "Blue"
)

You can find many usefull templated here:
https://www.daxpatterns.com/month-related-calculations/
Moving average 3 months
VAR MonthsInRange = 3
VAR LastMonthRange =
MAX ( 'Date'[Year Month Number] )
VAR FirstMonthRange =
LastMonthRange - MonthsInRange + 1
VAR Period3M =
FILTER (
ALL ( 'Date'[Year Month Number] ),
'Date'[Year Month Number] >= FirstMonthRange
&& 'Date'[Year Month Number] <= LastMonthRange
)
VAR Result =
IF (
COUNTROWS ( Period3M ) >= MonthsInRange,
CALCULATE (
AVERAGEX ( Period3M, [Sales Amount] ),
REMOVEFILTERS ( 'Date' )
)
)
RETURN
Result

Related

Rewrite dynamic T-SQL date variables in DAX

We're currently rebuilding basic emailed reports built using T-SQL to be paginated reports published on Power BI.
We're muddling through by creating the tables we need with the appropriate filters in Power BI Desktop to reconcile the numbers, then taking the DAX code from them using the Performance Analyser.
The one I'm working at the minute has a simple bit of SQL code to get data for a previous calendar month. I have no idea how or if it's possible for this to exist in DAX?
-- Validation to get previous month
IF (MONTH(GETDATE()) - 1) > 0
SET #MONTH = MONTH(GETDATE()) - 1
ELSE
SET #MONTH = '12'
-- Validation to get year of previous month
IF (#MONTH < 12)
SET #YEAR = YEAR(GETDATE())
ELSE
SET #YEAR = YEAR(GETDATE()) - 1
-- Set start date and finish date for extract
SET #PERIOD = #YEAR + RIGHT('00' + #MONTH, 2)
It needs to become a hidden SSRS parameter or just inline code to be used with this DAX variable:
VAR __DS0FilterTable =
TREATAS({"202212"}, 'Org View_VaultexCalendar'[Calendar Month No])
So the "202212" would become #period or the equivalent if doable without a parameter.
SSRS Parameter:
=IIF(
Month(Today()) > 1,
Year(Today()) & RIGHT("00" & Month(Today()) - 1, 2),
Year(Today())-1 & "12"
)
DAX expression:
IF (
MONTH ( TODAY () ) > 1,
YEAR ( TODAY () ) & FORMAT ( MONTH ( TODAY () ) - 1, "00" ),
YEAR ( TODAY () ) - 1 & "12"
)
In both cases we look at today's month and check if it's after January. If it is, take the current year and concatenate it with the current month less one and padded with a leading zero when needed. In the other case we know that the month is January so take the current year less one and concatenate it with "12"

DAX Calculate Billing Days Between Two Variable Dates

I have a dimdate table that is represented below. I have each day flagged as BusinessDay Y/N. I also have a DimSalesRep table that has a daily goal for each rep. I want to be able to allow users to input a StartDt and EndDt with filters on the report and have a calculated column look at the business days between those dates. I can calculate daysbetween with defined dates but I am unsure how I would use DAX with variable dates that are applied through Report filters.
I should also note I am not sure how best to handle a startdt and enddt filter based of the column, TheDate
Cheers!
Reference your dimdate table twice
StartDate = 'dimdate'
EndDate = 'dimdate'
and use this measure:
Num BusinessDays =
CALCULATE(
COUNTROWS('dimdate'),
'dimdate'[BusinessDay] = "Y",
'dimdate'[Date] >= SELECTEDVALUE(StartDate[Date]),
'dimdate'[Date] <= SELECTEDVALUE(EndDate[Date])
)

Display Data between Dec Y-1 and Dec current Y in PowerBI

I am new in powerBI, I loonking to display data in during a closing period and in my case it is from :
31/12/Y-1 to 31/12/Y my issue it is with slicer the year filter all date in the current year and not taking into account the value in 31/12/Y-1
How can I set PowerBI to do so,
thanks for your support
Display Sum of data in a defined period 31/12/Y-1 till 31/12/Y, user can select the period that they want to display and the data will update via PowerBI
Slicer below :
Use this calculated column
Closing Period =
VAR thisYear = YEAR(TODAY())
VAR startDate = DATE(thisYear -1 , 12, 31)
VAR endDate = DATE(thisYear, 12, 31)
RETURN
IF(startDate <= 'Date'[Date] && 'Date'[Date] <= endDate, thisYear)
and filter the 'Date'[Closing Period] column on the current year.
You can use a new calculated column for the slicer in your case:
Filter_date = DATEADD ( 'Date Table'[date].[Date], +1, DAY )

Create calculated column to add + work days to a date

I am trying to create a calculated column to add + X work days to a date based on a work day steering table.
In the work day steering table, the bank days are flagged as 0.
What DAX formula should I use to create the calculated column and shift the date + work days further?
Expected Result:
Work day steering table:
You can use either a Measure or Calculated Column code as given below-
Measure Code
add_day_dinamically =
MAXX(
TOPN(
MIN(your_table_name[transport_lead_time]),
FILTER(
all(work_day_steering_table),
work_day_steering_table[flag] = 1
&& work_day_steering_table[date].[Date] > MIN(your_table_name[date_column_name])
),
work_day_steering_table[date].[Date],
ASC
),
work_day_steering_table[date].[Date]
)
Calculated Column Code
add_day_dinamically_column =
MAXX(
TOPN(
your_table_name[transport_lead_time],
FILTER(
all(work_day_steering_table),
work_day_steering_table[flag] = 1
&& work_day_steering_table[date].[Date] > your_table_name[date_column_name].[Date]
),
work_day_steering_table[date].[Date],
ASC
),
work_day_steering_table[date].[Date]
)
Here is the output-

Power BI Rolling Average DAX to plot correctly on Column Chart

I have a problem with the measure of the 3mth rolling average to visualise it correctly on the graph.
The data model is here:
https://docs.google.com/spreadsheets/d/1naChcuZtjSbk0pVEi1xKuTZhSY7Rpabc0OCbmxowQME/edit?usp=sharing
I am using the formula below to calculate 3mth average through a measure:
Product3Mth = CALCULATE(SUM('Table'[Product A uncum]);DATESINPERIOD('Table'[Date];LASTDATE('Table'[Date]);-3;MONTH))/3
When I am plotting it as a table it is showing right values for each month.
But When I am plotting it in the column chart together with Product A Accumulated I am getting wrong value which is the value for Product unaccum /3 insted of sum of 3 consecutive values for Product unaccum /3.
What should I change in the DAX to have it visualised correctly? Please HELP
It seems you've stumble accross quite a few Power BI "gotchas" here when it comes to both the format of the date in your source data and the way you've chosen to display the Date column in your visulaization. But I think I've figured it out. This is my result:
And just to verify some numbers:
(4043 + 20 + 158) / 3 = 1469
(189+ 200 + 207) / 3 = 199
And here are the details:
I used this dataset where I've changed the names slightly to make it easier to write DAX expressions and imported it using Get Data
Date unAcc ACc
01-10-2017 00:00 4043 4043
01-11-2017 00:00 205 4248
01-12-2017 00:00 158 4406
01-01-2018 00:00 142 4548
01-02-2018 00:00 312 4860
01-03-2018 00:00 258 5118
01-04-2018 00:00 176 5294
01-05-2018 00:00 210 5504
01-06-2018 00:00 189 5693
01-07-2018 00:00 200 5893
01-08-2018 00:00 207 6100
And for reasons still uknown to me, I had the same issues as you had with the Date column. But following some tips from the Power BI community, I created a Date2 like this :
Date2 =
DATE('Table1'[Date].[Year];'Table1'[Date].[MonthNo];1)
Then I calculated the three month average using a
Moving_Average_3_Months =
CALCULATE (
AVERAGEX ( 'Table1'; 'Table1'[unAcc] );
DATESINPERIOD (
'Table1'[Date2];
LASTDATE ( 'Table1'[Date2]);
-3;
MONTH
)
)
Now, if you insert a column chart and assign Date2 to the Axis and Moving_Average_3_months together with unAcc to Values, you'll get this:
And that's not what we want. So go to the Visualization settings and change Date2 from Date Hierarchy to simply Date2 like this:
And that's it:
And here's the whole thing as a table so you can see that the numbers are correct:
In your case, maybe the only thing you have to do is that very last part.
Please don't hesitate to let me know how it works out for you!
I ask in the last comment the issue about the calculated value (as column or measure) because I have different results based on this, as you can see in the example below:
The Product3Mth is based on the calculated column and the Product3Mth 2 is based on the measure (result you want!).
Hope this example can help you. If not tell me please!
The DATESINPERIOD and LASTDATE functions will not work correctly, because 'Table'[Date] is not a continous range of dates. These functions need to be based on a datetable.
Try something like this
Product3MthAVG =
VAR rowDate = 'Table'[Date]
RETURN
CALCULATE (
SUM ( 'Table'[Product A unaccum] );
FILTER (
'Table';
'Table'[Date]
< DATE ( YEAR ( rowDate ); MONTH ( rowDate ) + 1; DAY ( rowDate ) )
&& 'Table'[Date]
>= DATE ( YEAR ( rowDate ); MONTH ( rowDate ) - 2; DAY ( rowDate ) )
)
)
/ 3