I am writing a DAX query to be used while performing a Power BI SQL Server Analysis Services database Import. I want to limit the fields and records to be used. I cannot figure out how to include measures in my query. Any help would be appreciated!
This works:
EVALUATE (
SUMMARIZECOLUMNS (
'Date PT'[Calendar Date PT],
'Date PT'[Fiscal Month PT],
'Date PT'[Fiscal Year PT],
'Issue Code'[Code 2],
'Planning Cat'[Planning Area],
FILTER('Date PT', 'Date PT'[Fiscal Year PT]= "FY22"),
FILTER('Planning Cat', [Planning Area]= "NOA")
)
)
I need to add in Sales by Booked Date (a measure) and I get an error stating that this field "cannot be found or cannot be used in this expression"
THIS DOES NOT WORK
EVALUATE (
SUMMARIZECOLUMNS (
'Date PT'[Calendar Date PT],
'Date PT'[Fiscal Month PT],
'Date PT'[Fiscal Year PT],
'Issue Code'[Code 2],
'Planning Cat'[Planning Area],
'Sales'[Sales by Booked Date],
FILTER('Date PT', 'Date PT'[Fiscal Year PT]= "FY22"),
FILTER('Planning Cat', [Planning Area]= "NOA")
)
)
I have tried everything I can find but nothing seems to work. If I leave a measure out, it works as expected.
Try this way:
EVALUATE
ADDCOLUMNS(
SUMMARIZECOLUMNS (
'Date PT'[Calendar Date PT],
'Date PT'[Fiscal Month PT],
'Date PT'[Fiscal Year PT],
'Issue Code'[Code 2],
'Planning Cat'[Planning Area],
FILTER(
'Date PT',
'Date PT'[Fiscal Year PT]= "FY22"
),
FILTER(
'Planning Cat',
[Planning Area]= "NOA"
)
),
"#SalesByBookedDate", CALCULATE ( 'Sales'[Sales by Booked Date] )
)
Related
I am trying to set up a report on conversions this year vs last year. This formula has worked on another report but cannot show the conversions from last year when applied to this client.
When just filtering on a particular week last year, it shows conversions, so there must be something wrong with my measure.
LY Conversion =
CALCULATE(
SUM('Krogh_Søksrapport'[All_Conversions]),
PREVIOUSYEAR('Date table'[Date]),
'Date table'[ISOWeeknumber] IN VALUES('Date table'[ISOWeeknumber]))
Any obvious errors? I've tried to dismantle it piece by piece but can't find the error.
All help is appreciated :)
UPDATE for Ozan:
enter image description here
We have a date table with one-to-many relationship
Date table looks like this:
enter image description here
SOLVED: All we had to do was mark the date table as the the date table, so that the "previousyear"-function used our date table for its date logic.
All we had to do was
mark the date table as the the date table, so that the "previousyear"-function used our date table for its date logic.
Thats it!
Man beat machine this day!
Please test this one:
Update()
Version_01
LY Conversion =
CALCULATE (
SUM ( 'Krogh_Søksrapport'[All_Conversions] ),
FILTER (
ALL ( 'Date table'[Date], 'Date table'[ISOWeeknumber] ),
SAMEPERIODLASTYEAR ( 'Date table'[Date] )
&& 'Date table'[ISOWeeknumber] IN VALUES ( 'Date table'[ISOWeeknumber] )
)
)
Version_02
LY Conversion =
CALCULATE (
SUM ( 'Krogh_Søksrapport'[All_Conversions] ),
CALCULATETABLE (
ALL ( 'Date table'[Date], 'Date table'[ISOWeeknumber] ),
SAMEPERIODLASTYEAR ( 'Date table'[Date] ),
'Date table'[ISOWeeknumber] IN VALUES ( 'Date table'[ISOWeeknumber] )
)
)
I have a measure that depending on a "before" date slicer shows how many accounts were active at any given point in the company's history. I'm being asked to show month over month growth (end of month 1 compared to end of month 2 totals) but that's difficult given my measure needs a date slicer with one date value to return a total.
Active_Accounts =
CALCULATE (
COUNTX (
FILTER (
VALUES ( 'TEST CHARGES'[BI_ACCT] ),
[total as of date] > 0
),
[BI_ACCT]
)
)
link to sample file
https://www.dropbox.com/s/pewpm85wogvq3xf/test%20active%20charges.pbix?dl=0
if you move the slider you'll see the active accounts total change to show at that time in history how many accounts had an active charge. What I'm hoping to add to the dashboard is a measure that can be placed on a table of month end values and show the active accounts at that time so I can do month to month comparisons.
Active Accounts =
var month_end =
ENDOFMONTH (
LASTNONBLANK (
'Test Charges Date Table'[Date],
CALCULATE ( DISTINCTCOUNT( ( 'TEST CHARGES'[BI_ACCT] ) )
)
)
)
var last_date =
CALCULATE(
LASTNONBLANK('TEST CHARGES'[CHG_DATE], ""),
'TEST CHARGES'[CHG_DATE] <= max('Test Charges Date Table'[Date])
)
var num_of_actives =
CALCULATE(
Countx(
Filter(
Values('TEST CHARGES'[BI_ACCT]),
[total as of date] > 0
) , [BI_ACCT]
),
last_date <= month_end
)
return num_of_actives
As Peter advices you do not need Calculate() to show total in the card and using of Calculate() reduces speed of calculation. In your case speed is reduced with no reason.
There are no need to have a special column for month - use date hierarchy for row and just exclude day and quater levels.
Then add the measure to the visual table/matrix
Cummulative Count =
Calculate(
[Active_Accounts]
,'Test Charges Date Table'[Date]<=MAX('Test Charges Date Table'[Date])
)
Cummulative Count prevMonth =
Calculate(
[Cummulative Count]
,PARALLELPERIOD('Test Charges Date Table'[Date],-1,MONTH)
)
I have a measure that sums expense amount for the current filtered year. This works as desired.
Expense = CALCULATE(SUM('GL Data'[Amount]),FILTER('GL Data','GL Data'[SubGrp1] = "EXPENSE"))
I want to sum the expense amount for the same time period of prior year. My measure calculates correct data as of 11 months ago, not 1 year ago. It appears to function as if the data calculation is looking at my system date, not the specified date:
Expense PY = CALCULATE([Expense], DATEADD(Dates[Date],-1,YEAR))
I've attempted using -12, -13 and MONTH in the DATEADD but the result doesn't correctly total 1 year ago. I've also tried SAMEPERIODLASTYEAR() which provides wrong info. Perhaps my Expense measure is the culprit.
My Date table is generated with this Dax:
Could you please use this as your date table, and test it. In your date table, you imposed no restrictions on the calendar auto() functions which I think is not useful. Also You used Generate function which ignores rows with blank values. In such a case, use GENERATEALL() function; but You need to form your date table like this:
Date =
VAR MinYear =
YEAR ( MIN ( Fact_Table[Date_Column] ) )
VAR MaxYear =
YEAR ( MAX ( Fact_Table[Date_Column] ) )
RETURN
ADDCOLUMNS (
FILTER (
CALENDARAUTO ( 12 ),
AND ( YEAR ( [Date] ) >= MinYear, YEAR ( [Date] ) <= MaxYear )
),
"Calendar Year", "CY " & YEAR ( [Date] ),
"Day", FORMAT ( [Date], "dddd" ),
"Month Number", MONTH ( [Date] ),
"Month", FORMAT ( [Date], "mmmm" ),
"Year Month", FORMAT ( [Date], "mmm yy" ),
"Year", FORMAT ( [Date], "YYYY" )
)
After creating your Date Table, please do not forget to mark it as date table which is a good DAX practice as in the photo below.
I'm trying to calculate MAT (Moving Annual Total) using DAX, but can't realize exactly what I want.
I have the data as below.
What I'm trying to is calculate the total of treatment for each 12 month, like "Jan 21 - Dec 21" which has 'date' from 2020/1/1 to 2020/12/1. i.e. 'A' appears 4 times in that period, so the result will be like below.
Then I'd like to continue this for each latest 12 months, and finally visualize it like below.
I've read that I should use CALCULATE and DATESPERIOD in DAX, can't calculate exactly though. The following code is the one I tried and failed.
Moving Annual Total = CALCULATE(
COUNTA('2017-2022Q1'[idnum]),
DATESINPERIOD('2017-2022Q1'[mat].[Date], LASTDATE('2017-2022Q1'[mat].[Date]), 12, MONTH))
Could someone kindly give me an advise to realize this?
Thanks.
*This is the sample file.
https://drive.google.com/file/d/1gDNeBe5KiKBqx3cZ7G0SMiSQ23w96NF4/view?usp=sharing
In your dax measure, I recommend you first to create a date table and create a one-to-many relationship with the fact table(your table: '2017-2022Q1') ; then filter the date table in your calculate measure, not the fact table('2017-2022Q1') directly as you did here. You should follow the best practice.
For Example: How to create a date table using dax:
Date =
VAR MinYear = YEAR ( MIN ( '2017-2022Q1'[date] ) )
VAR MaxYear = YEAR ( MAX ( '2017-2022Q1'[date] ) )
RETURN
ADDCOLUMNS (
FILTER (
CALENDARAUTO( ),
AND ( YEAR ( [Date] ) >= MinYear, YEAR ( [Date] ) <= MaxYear )
),
"Calendar Year", "CY " & YEAR ( [Date] ),
"Month Name", FORMAT ( [Date], "mmmm" ),
"Month Number", MONTH ( [Date] )
)
Your final code could be like this:
Moving Annual Total = CALCULATE(
COUNTA('2017-2022Q1'[idnum]),
DATESINPERIOD('Date'[Date], LASTDATE('Date'[Date]), -12, MONTH)
)
if you define the start date using the lastdate() function, then It returns the last date in your filter context. you should also specify the interval as (-12) month to extract all dates within 1 year period.
Hope It helps.
I am able to generate calendar table for each month, but it goes till the end of current month.
CalendarTest =
SUMMARIZE(
ADDCOLUMNS(
CALENDAR ("01-01-2018", TODAY()),
"Month", EOMONTH([Date], - 1) + 1
),
[Month],
"Eomonth",EOMONTH([Month],0)
But the last value for Eomonth should be today's date. Not the end of the month.
Need it to be like that:
Try
CalendarTest =
SUMMARIZE(
ADDCOLUMNS(
CALENDAR ("01-01-2018", TODAY() ),
"Month", EOMONTH ( [Date], - 1) + 1
),
[Month],
"Eomonth", MIN ( EOMONTH([Month],0), TODAY() )
)