Dax Studio - The syntax for Calculate is incorrect - powerbi

I am learning to use DAX Studio to test out my DAX scripts,
DAX Studio says my script is wrong, but I am not sure what exactly is the problem
CummulativeWeek =
CALCULATE (
SUM ( Data[Value] ),
FILTER ( ALL ( Data[Week] ), Data[Week] <= MAX ( Data[Week] ) ),
FILTER ( ALL ( Data[Year] ), Data[Year] == MAX ( Data[Year] ) )
)
I don't see what I am doing wrong. I checked the docummentation, YouTube vids, everything seems to be fine.
In PowerBI the command is executed just fine.
Is there a setting in DAX Studio that I am missing?

Are you using the EVALUATE keyword?
Also, you need to return a table.
Have a read here: https://daxstudio.org/docs/tutorials/writing-dax-queries/

Related

AVERAGEX with a measure

We're a trucking company. I have a measure that gives me a total number of drivers within a date range context. I really need an average. My thought was to use AVERAGEX to create the average utilizing the count measure I created.
I have not had much luck. There is enough internet resources to say it should be possible, but I can't seem to put the components together.
This is my Driver Count measure.
Driver Count (Company) =
CALCULATE (
COUNTROWS ( Drivers ),
FILTER (
VALUES ( 'Drivers'[Hire Date] ),
'Drivers'[Hire Date] <= MAX ( Dates[Date] )
),
FILTER (
VALUES ( 'Drivers'[Termination Date] ),
OR(
Drivers[Termination Date] >= MIN ( Dates[Date] ),
ISBLANK ('Drivers'[Termination Date] )
)
),
FILTER(
Drivers,
Drivers[Activated] = "Y"
),
FILTER(
Drivers,
Drivers[Type] = "Company"
)
)
And this is the driver table I'm working with...
Any resources you can point me to in order to learn what I need to know would be greatly appreciated. I would also welcome alternative methods (like making transforming that driver count measure into a driver average measure).
I tried this adapted from a similar problem solved here on Stack Overflow, but I have to be honest and did not expect it to work.
Avg Driver Count (Company) =
AVERAGEX(VALUES(Drivers[Code]),
[Driver Count (Company)])
It resulted in '1.00' for every date range. That makes sense to me, actually. So, I suspect I have the wrong value in 'VALUES'.

How to count dates in Power BI DAX

I have a table like below.
I want to show count of LicenseEndDate. Like In my slicer I have taken WeekEnding column suppose if I select weekending date like 09/25/2022 then I want show the count of LicenseEndDate dates that are ended between 09/18/2022 to 09/25/2022. How to create measure for this. I want to show that count in card visual. I created a measure like below
License_Expired_Count = COUNT(Trade[LicenseEndDate]).
But It giving me count of all. That mean the License which are expiring in the feature count also it was showing. But want to show count of license which are expired in the selected period. How create measure for this.
Please check this code, let me know If It works for you.
License_Expired_Count =
CALCULATE (
COUNT ( Trade[LicenseEndDate] ),
Trade[LicenseEndDate] <= SELECTEDVALUE ( Trade[WeekEndingDate] ),
Trade[LicenseEndDate]
> SELECTEDVALUE ( Trade[WeekEndingDate] ) - 7
)
try this :
License_Expired_Count =
VAR _slt =
SELECTEDVALUE ( Trade[WeekEndingDate] )
RETURN
CALCULATE (
COUNT ( Trade[LicenseEndDate] ),
ALLSELECTED ( Trade[WeekEndingDate] ),
DATESBETWEEN ( Trade[LicenseEndDate], _slt - 7, _slt )
)
check sample file attached

Hide a visual until slicers are selected in power BI

I want to display values on the table visual only when 3 of the slicers are selected or else no values should be displayed. For this, I did :
IF(ISFILTERED(Table[Country]),IF(ISFILTERED(Table[Procurement Team]),IF(ISFILTERED(Table[Class]),1,0),0),0)
Then in the Filters panel I put this measure and did "is" 1.
This means when the slicers are selected, i.e. TRUE then the values will be showed however this is working fine till procurement team selection but as soon as I am selecting class , the values in the table get populated. I am unable to understand why. Can anyone help me with this?
I think the problem is that you are using some of the fields inside the visual. Removing the filter context created by the visual might be a possible solution. To achieve this, use ALLSELECTEDand CALCULATE
ToShow =
CALCULATE(
IF(
ISFILTERED( 'Table'[Country] ),
IF(
ISFILTERED( 'Table'[Procurement Team] ),
IF( ISFILTERED( 'Table'[Class] ), 1, 0 ),
0
),
0
),
ALLSELECTED( 'Table' )
)
and then you can implement your measures testing the [ToShow] like for instance
Sum Value =
IF( [ToShow] = 1, SUM( 'Table'[Value] ) )

Power BI - DistinctCount with conditions does not working

I have a table and I need to add the different values that are greater than 5 and belong to the DEV team.
Unfortunately, it is not working.
I used this code, the expected result is 6 but now is showing only 3.
CALCULATE(
DISTINCTCOUNT('data (1)'[issue_key]);
FILTER('data (1)';[team]="Dev");
FILTER('data (1)';[SLAs]>5)
)
I'm guessing this has to do with the row-context to filter-context transition induced by CALCULATE.
Try removing the row context with ALL:
CALCULATE (
DISTINCTCOUNT ( 'data (1)'[issue_key] );
FILTER ( ALL ( 'data (1)' ); [team] = "Dev" && [SLAs] > 5 )
)

DAX measure not working when RLS is turned on - Join paths are expected to form a tree

In Power BI, I have the following error for a measure when RLS is turned on (this error does not show when RLS is off):
Join paths are expected to form a tree but the table has two join paths
These are the relevant relationships in the model:
I have an inactive relationship. This inactive relationship is used in the measure with the problem. But as it is inactive, I would have thought it wouldnt be an issue?? The measure is:
TTipsInvs =
VAR SalesValue =
CALCULATE (
SUM ( ANSAPBICustomerTransDetailed[Outstanding] ),
USERELATIONSHIP ( 'ANSAPBICustomerTransDetailed'[SiteID], ANSAPBISites[Site ID] )
)
RETURN
IF ( ISBLANK ( SalesValue ), 0, ( SalesValue ) )
Any way to avoid this issue when RLS is turned on?
Cheers for all help
TTipsInvs =
VAR SiteID =
CALCULATETABLE (
VALUES ( ANSAPBISites[Site ID] ) )
VAR SalesValue =
CALCULATE (
SUM ( ANSAPBICustomerTransDetailed[Outstanding] ),
TREATAS ( SiteID, 'ANSAPBICustomerTransDetailed'[SiteID] ) )
RETURN
IF ( ISBLANK ( SalesValue ), 0, ( SalesValue ) )
I'd like to add to #user9824134's answer that unfortunately did not have much of an explanation (cannot edit).
Why TREATAS:
The docs explain that this function applies the result of the first argument (clientID) on the column in the second argument ('ANSAPBICustomerTransDetailed'[SiteID])
Why this works:
Not too sure how RLS enforces the joint path, but the "always-on" idea explained by #asp8811 makes sense to me.
The TREATAS is essentially working as an alternative to CROSSFILTER in this case. By applying the filter of clientID on the SiteID column of the ANSAPBICustomerTransDetailed table, it is emulating CROSSFILTER.
I'd have to test efficiency, but I suspect performance should be similar as VALUES already only returns a distinct list.
The below approach also works and is much shorter. The CALCULATETABLE expression does nothing if a filter is not passed to it, so simply using VALUES is enough.
TTipsInvs =
VAR clientID = VALUES ( ANSAPBISites[Site ID] )
VAR SalesValue =
CALCULATE (
SUM ( ANSAPBICustomerTransDetailed[Outstanding] ),
TREATAS ( clientID, 'ANSAPBICustomerTransDetailed'[SiteID] ) )
RETURN
IF ( ISBLANK ( SalesValue ), 0, ( SalesValue ) )
Did a quick test:
And this looks to work without the need of enabling or disabling relationships.
Before RLS:
After RLS: