I need to write a DAX measure that calculates (e.g., "Count Rows"), but only when another measure value is evaluated (e.g., filtering "[Sales]>100"). So if-- in the context of the selected filters-- Sales is great than 100, then the measure is executed only for those rows.
The measure I have defined works in the context of lower smaller grain. But the totals do not sum correctly.
Any suggestions?
DAX Measure
License Usage =
// Users with active viewership in 3 months
IF (
NOT ( ISBLANK (
CALCULATE (
[Activity Date NEWEST],
KEEPFILTERS ( DATESINPERIOD ( dimCalendar[Date], TODAY (), -90, DAY ) )
)
)), 1
)
Activity Date NEWEST =
MAX('PBI Activity'[Date])
Okay, I figured something out that works.
DAX
License Usage =
// Users with active viewership in 3 months
CALCULATE (
[Count Users],
FILTER ( 'PBI Activity', 'PBI Activity'[Date] >= TODAY () - 90 )
)
Count Users = COUNTROWS('Users')
Also, I later came across this article which looks like it also does what I was hoping to do: Execute calculate expression over filtered rows based upon measure filter.
Reference: Specifying multiple filter conditions in CALCULATE - SQLBI
DAX
DEFINE
MEASURE Sales[Big Sales Amount] =
CALCULATE (
[Sales Amount],
KEEPFILTERS (
FILTER (
ALL ( Sales[Quantity], Sales[Net Price] ),
Sales[Quantity] * Sales[Net Price] > 1000
)
)
)
EVALUATE
SUMMARIZECOLUMNS (
Sales[Quantity],
"Sales Amount", [Sales Amount],
"Big Sales Amount", [Big Sales Amount]
)
I have a problem of getting the percentage on customer sales in matrix table thta is sliced by store_id and employee_id using ALLEXCEPT function. The percentage of customer sales need to obrain from dividing customer sales measure in current filter by the same measure of the respective store_id, instead of total customer sales of all the store_id.
Here is my expectation of the output and the DAX when using ALL function on the staff_id.
% of Customer Sales =
VAR ALLExceptSales =
CALCULATE(
[Customer Sales],
ALL(
'Employee Lookup'[staff_id]
)
)
VAR Ratio =
DIVIDE(
[Customer Sales],
ALLExceptSales,
BLANK()
)
RETURN Ratio
I have tried using ALLEXCEPT on store_id from sales by store table, ALLEXCEPT store_id from store lookup table and ALLEXCEPT on both the tables, still giving me different output.
Here is the pbix file for testing.
https://drive.google.com/file/d/1fRrfsikHl9aK06GzAozJ9Wc16Ue0YJm2/view?usp=sharing
Anyone can hint me on?
Replace DAX with following
% of Customer Sales =
VAR ALLExceptSales =
CALCULATE(
sum(Table[Customer Sales]),
ALLEXCEPT(Table, 'Table'[store_id])
)
VAR Ratio =
DIVIDE(
sum(Table[Customer Sales]),
ALLExceptSales,
BLANK()
)
RETURN Ratio
Have you tried ALLSELECTED instead of ALLEXCEPT?
% of Customer Sales =
DIVIDE (
[Customer Sales],
CALCULATE ( [Customer Sales], ALLSELECTED ( 'Employee Lookup'[staff_id] ) )
)
I have a FIXED LOD with multiple dimensions that looks likes this:
{ FIXED [Customer ID], [Quarter], [Product Type] : SUM([Sales]) }
In Power BI I have wrote it as below using the example given in this article:
CALCULATE(
SUM('Table'[Sales]),
ALLEXCEPT('Table', 'Table'[Customer ID]),
ALLEXCEPT('Table', 'Table'[Quarter]),
ALLEXCEPT('Table', 'Table'[Product Type])
)
However it is not working as expected, could you help me understand the correct way of doing it?
I found the answer by editing my original expression, the following worked as expected:
CALCULATE(
SUM('Table'[Sales]),
ALLEXCEPT('Table', 'Table'[Customer ID], 'Table'[Quarter], 'Table'[Product Type])
)
I have salesman dimension and sales fact with 1:M relationship on salesman_id.
I am trying to create a measure for count of salesman that have made sales in location is 6.
CALCULATE (
DISTINCTCOUNT ( Salesman[SalesmanKey] ),
Sales,
Sales[LocationId] = 6
)
I think this is not working because the filter doesn't flow from sales into salesman table.
I could change the filter direction as both but I'm looking at other option like using DAX CALCULATE with CROSSFILTER. Is there any other option like using CALCULATETABLE?
You can use the following Dax formula to achieve your goal:
Measure =
CALCULATE(
DISTINCTCOUNT( Salesman[SalesmanKey] ),
CROSSFILTER( Sales[salesman_id], Salesman[SalesmanKey], Both ),
Sales[LocationId] = 6
)
However I recomend you using the salesman id from the fact table:
Measure =
CALCULATE(
DISTINCTCOUNT( Sales[salesman_id] ),
Sales[LocationId] = 6
)
Edit: i added the option using the calculatetable:
Measure =
CALCULATE(
DISTINCTCOUNT( Salesman[SalesmnaKey] ),
CALCULATETABLE(
Sales,
Sales[LotacionId] = 6
)
)
I need to find moving two days sum of sales. I am using DAX function DatesinPeriod but the output is not coming correct. Please help me understand where I am going wrong please. I am using below Dax Formula:
Measure = CALCULATE(sum('Table'[Sale]),DATESINPERIOD('Dim Date'[Date],SELECTEDVALUE('Table'[Date]),-2,day))
To replicate the scenario first step is to create Dim Date table using - >
Dim Date = GENERATESERIES(date(2019,01,01),date(2019,12,31),1)
second Step is to create DataTable ->
Table = DATATABLE("Date",DATETIME,"Flag1",STRING,"Flag2",STRING,"Sale",INTEGER,{
{"8/1/2019","True","True",200},
{"8/2/2019","False","True",80},
{"8/2/2019","False","True",80},
{"8/2/2019","False","True",80},
{"8/2/2019","False","True",80},
{"8/2/2019","False","True",80},
{"9/3/2019","False","True",60},
{"9/4/2019","False","True",10},
{"9/5/2019","False","True",100},
{"9/6/2019","False","True",30},
{"9/7/2019","False","True",60},
{"9/8/2019","False","False",150},
{"9/9/2019","False","False",80},
{"9/10/2019","False","False",90},
{"9/11/2019","False","False",30},
{"9/12/2019","False","False",20},
{"10/13/2019","False","True",50},
{"10/14/2019","False","True",60},
{"10/15/2019",BLANK(),BLANK(),BLANK()},
{"10/16/2019",BLANK(),BLANK(),BLANK()}
})
3rd Step - create a relation between these tables on date column
4th step - create Measure using - Measure = CALCULATE(sum('Table'[Sale]),DATESINPERIOD('Dim Date'[Date],SELECTEDVALUE('Table'[Date]),-2,day))
You will see the output coming wrong. see the screenshot. This is very strange. I tried using DatesBetween function , its also giving me the same wrong output.
Use the following measure to obtain the expected result:
SumInRange =
VAR __selectedDate = SELECTEDVALUE( 'Table'[Date] )
VAR __subTable =
FILTER(
ALL( 'Table'[Date] ),
AND(
'Table'[Date] >= __selectedDate -2,
'Table'[Date] <=__selectedDate
)
)
Return
CALCULATE(
SUMX (
DISTINCT ( 'Table'[Date] ),
CALCULATE ( MAX ( 'Table'[Sale] ) )
),
__subTable
)
Be sure to use the Date column from Table instead of the Dim in the visualization.