If a record doesn't not exist in table (filters applied), I want my && function to return 1 in DAX measure - dax

I have added image which will describe data and matrix i want to create
I have to use simple DAX to show if vehicle has taken a trip and it has not refueled that day. So it should return 1 in my measure.
I am using IF Table 1[Miles]>1 && Table2 [fuel]<0 , it should return 1, filters (Date and Vehicle Number). it returns blank in my matrix
The day vehicle doesnt refill , no record is entered, so if i check data behind, apply date filter , i donot get any rows.
I want my DAX to return 1 if it doesnt find any record

According to the fuel not being negative, At most zero, and based on this condition:
Here is the relationship formed [Vehicle ID]:
Also you need to enter data for Car ID2 Fuel information:
Now You need to create your measure to use in matrix visual:
YourMeasure = CALCULATE(
SUMX('Data Table2',
IF(RELATED('Data Table1'[Miles])>1 && 'Data Table2'[Fuel]= 0, 1)))
And finally test it on the visual:

Related

Tips to sum values and ignore all filter but the fields of two table in Dax?

I have 3 dimensions tables and one fact Table Sales
DimCalendar (Fields Year/Month/Day/Week)
DimCountry (Field : CountryName)
DimManager (Field ManagerName)
FctSales (Field : Amount)
I want to create a measure to Sum the Amount of the Sales (FctSales) and filter only to the fields of the tables DimCalendar and DimCountry.
After research, i was thinking about the function AllExcept, like :
CALCULATE(SUM(Sales[Amt]);ALLExcept(Sales;Country[Country];Calendar[Year]...)
but if i do that, i will have to write every columns of the table Calendar and Table Country in the AllExcept, i am wondering if there is another solution.
Maybe using REMOVEFILTERS() to remove every filter and then put back the filters over DimCountry and DimCalendar might work?
CALCULATE (
SUM ( Sales[Amt] );
REMOVEFILTERS ();
VALUES( DimCountry[CountryName] );
VALUES( DimCalendar[Date] )
)
DimCalendar[Date] should be the column used for the relationship with Sales.
This measure first evaluates the filter arguments in the current filter context.
Using as filter the columns used for the relationships guarantees that whatever the column used for filtering this would be mapped over the relationship.
Then, the REMOVEFILTERS() will remove any existing context filter and eventually the filter arguments evaluated during the first step will be applied, putting back any filtering that was set over DimCalendar and DimCountry.

How to filter by measure values in MDX while having dimension members in both axis

I'm developing an application that uses a tabular database to show some business data.
I need to provide some basic filtering over measures values (equal to, greater than, lesser than etc.) and I'm currently analyzing the proper way to generate the MDX.
Looking at some documentation (and other threads on this site), I found that the most efficient approach would be using the FILTER or HAVING functions to filter out undesired values.
Unfortunately all examples normally include measures on one axis and dimension member on the other, but I potentially have dimension members in both axis and can't find a proper solution to use such functions to filter by measure value.
What have I done so far?
To make it easier to explain, let's say that we want to get the yearly sales quantities by product class filtering quantity > 1.3 milions
Trying to use HAVING or FILTER Functions, the resulting MDX I came up with is
SELECT
NON EMPTY {[YearList].[Year].[Year].MEMBERS * [Measures].[Qty]}
HAVING [Measures].[Qty] > 1.3e6 ON COLUMNS,
NON EMPTY {[Classes].[cClass].[cClass].MEMBERS}
HAVING [Measures].[Qty] > 1.3e6 ON ROWS
FROM [Model]
or
SELECT
NON EMPTY FILTER({[YearList].[Year].[Year].MEMBERS * [Measures].[Qty]},
[Measures].[Qty] > 1.3e6) ON COLUMNS,
NON EMPTY FILTER({[Classes].[cClass].[cClass].MEMBERS} ,
[Measures].[Qty] > 1.3e6) ON ROWS
FROM [Model]
But this is of course leading to unexpected result for the final user because the filter is happening on the aggregation of the quantities by the dimension on that axis only, which is greater then 1.3M
The only way I found so far to achieve what I need is to define a custom member with an IIF statement
WITH
MEMBER [Measures].[FilteredQty] AS
IIF ( [Measures].[Qty] > 1.3e6, [Measures].[Qty], NULL)
SELECT
NON EMPTY {[YearList].[Year].[Year].MEMBERS * [Measures].[FilteredQty]} ON COLUMNS,
NON EMPTY {[Classes].[cClass].[cClass].MEMBERS} ON ROWS
FROM [Model]
The result is the one expected:
Is this the best approach or I should keep using FILTER and HAVING functions? Is there even a better approach I'm still missing?
Thanks
This is the best approach. You need to consider how MDX resolves result. In the example above it is a coincidence that your valid data in a continous region of first four columns of first row. Lets relax the filtering clause and make it >365000. Now take a look at last row of the result, the first two columns and the last column are eligible cells but the third and fourth column is not eligible. However your query will report it as null and the non empty function will not help. The reason is that non empty needs the entire row to be null
Now the question that why filter is not eliminating the cell? Filter will eliminate a row or column when the criteria is greater then the sum on the other axis. So if filter is on columns the filter value has to be greater than the sum of rows for that column. Take a look at the sample below as soon as you remove the comments the last column will be removed.
select
non empty
filter(
([Measures].[Internet Sales Amount]
,{[Date].[Calendar Year].&[2013],[Date].[Calendar Year].&[2014]}
,[Date].[Calendar Quarter of Year].[Calendar Quarter of Year]
),([Date].[Calendar Year].currentmember,[Date].[Calendar Quarter of Year].currentmember,[Product].[Subcategory].currentmember,[Measures].[Internet Sales Amount])>45694.70--+0.05
)
on columns
,
non empty
[Product].[Subcategory].members
on rows
from
[Adventure Works]
Edit another sample added.
with
member [Measures].[Internet Sales AmountTest]
as
iif(([Date].[Calendar Year].currentmember,[Date].[Calendar Quarter of Year].currentmember,[Product].[Subcategory].currentmember,[Measures].[Internet Sales Amount])>9000,
([Date].[Calendar Year].currentmember,[Date].[Calendar Quarter of Year].currentmember,[Product].[Subcategory].currentmember,[Measures].[Internet Sales Amount]),
null
)
select
non empty
({[Measures].[Internet Sales Amount],[Measures].[Internet Sales AmountTest]}
,{[Date].[Calendar Year].&[2013]}
,[Date].[Calendar Quarter of Year].[Calendar Quarter of Year]
)
on columns
,
non empty
[Product].[Subcategory].[Subcategory]
on rows
from
[Adventure Works]

How to get the difference between the values selected by slicer?

I am new to Power BI and currently I am working with table visulaizations and slicers.
My data is as follows
Student table:
Date table:
Exam table:
The relationships within the table are as follows:
I want an output like the image shown below, I would like to create 2 table visuals that can be filtered on Student Name, Classroom and also have slicer on 2 dates. I need to compute minimum score. The user must be able to select 2 dates at a time on the slicer, the first date selected on the slicer should be attached to my 'Min Score at date1' and second date selected on the slicer should be attached to my 'Min Score at date2', and the third column 'Difference in Score' must be able to calculate the difference between the Min Score at date1 and Min Score at date2.
Similarly I also want to calculate the average minimum score too
Please let me know how to proceed or what alternative formula or query or method should I apply to get the desired result.Thanks!
Before I start, let me mention that this example was done in SSAS so it may need some tweaking in PowerBi but the logic is identical nonetheless.
First create a clone date table and call it something else e.g. 'Compare Date'. Next, create an inactive, one to many relationship between the 'Compare Date' and your 'Fact' table, see the image below, in this case I am joining on [Year Month], you will need to adjust to fit your needs:
If you are unsure how to do this, just right click on the new table and select the create relationship option, ensure that the relationship is like the image below:
Once this has been done, right click on the 'relationship' and mark it as inactive.
Now that you have the new date table and the relationships set up, I want you to create a few DAX measures:
Min Date 1 = Min('Student Table'[Score])
Min Date 2 = CALCULATE(Min('Student Table'[Score]), ALL('Dates'), USERELATIONSHIP('Compare Date'[Date], 'Fact'[Date]))
Avg Date 1 = AVERAGE('Student Table'[Score])
Avg Date 2 = CALCULATE(AVERAGE('Student Table'[Score]), ALL('Dates'), USERELATIONSHIP('Compare Date'[Date], 'Fact'[Date]))
Delta Min = [Min Date 2] - [Min Date 1]
Delta Avg = [Avg Date 2] - [Avg Date 1]
These measures will calculate exactly what you need and can be filtered independently via two date slicers tied to each date table. The rest is just busy work.
I hope this helps.

Dax - Filter by value then count occurence in table

I am working with an employee table and was wondering if I could get some help.
My data table has rows with start and end date values. I filter these rows down using Filter(data table, [start date]<=[Measure.MaxMonth]&&[end date]>=[Measure.MaxMonth]. [Measure.MaxMonth] is a measure that sits on a disconnected date table and functions like a parameter.
Here is a formula that I have been testing but have not been getting the desired results:
Measure.DirectReports = CALCULATE(COUNTROWS(Data Table),filter(Data Table,Data Table[Mgr ID]=Data Table[Emp ID]&&Data Table[Start Date]<=[Meas.LastMonth]&&Data Table[End Date]>=[Meas.LastMonth]))
Measure.LastMonth = max(EOM[End of Month]) ->this value can equal any month end between July 2005 and July 2017. EOM is a date table with a row for each month end - 7/31/2005, 8/31/2005,....7/31/2017
This gives me a table structured liked this:
Emp ID,Emp Attr,Mgr ID,Start Date,End Date
1,B,4,10/1/2013,10/6/2013
1,B,4,10/7/2013,12/29/2013
1,B,4,12/30/2013,12/28/2014
1,B,8,12/29/2014,10/4/2015
1,B,8,10/5/2015,12/27/2015
1,B,12,12/28/2015,5/15/2016
1,B,12,5/16/2016,10/2/2016
1,B,12,10/3/2016,12/25/2016
2,B,4,12/1/2014,12/28/2014
2,B,4,12/29/2014,12/27/2015
2,B,4,12/28/2015,2/7/2016
2,B,4,2/8/2016,3/6/2016
2,B,8,3/7/2016,6/1/2016
3,B,6,7/1/2015,12/27/2015
3,B,8,12/28/2015,6/30/2016
3,B,6,7/1/2016,9/4/2016
3,B,6,9/5/2016,12/25/2016
3,B,6,12/26/2016,5/7/2017
3,B,4,5/8/2017,6/11/2017
3,B,4,6/12/2017,6/25/2017
3,B,4,6/26/2017,7/9/2017
3,B,19,7/10/2017,12/31/9999
4,A,,7/1/1996,4/2/2006
4,A,,4/3/2006,12/31/2007
4,A,,1/1/2008,5/22/2011
4,A,,5/23/2011,11/16/2014
4,A,,11/17/2014,6/11/2017
4,A,,6/12/2017,6/25/2017
4,A,,6/26/2017,12/31/9999
5,B,4,11/8/2010,1/2/2011
5,B,4,1/3/2011,5/22/2011
5,B,4,5/23/2011,1/1/2012
5,B,4,1/2/2012,5/31/2012
5,B,4,6/1/2012,7/1/2012
5,B,4,7/2/2012,9/7/2012
6,B,4,1/3/2011,5/22/2011
6,B,4,5/23/2011,9/5/2011
6,B,4,9/6/2011,1/1/2012
6,B,4,1/2/2012,12/30/2012
6,B,4,12/31/2012,12/29/2013
6,B,4,12/30/2013,5/18/2014
6,B,4,5/19/2014,11/16/2014
6,B,4,11/17/2014,12/28/2014
6,B,4,12/29/2014,3/22/2015
6,B,4,3/23/2015,12/27/2015
6,B,4,12/28/2015,3/6/2016
6,B,4,3/7/2016,8/21/2016
6,B,4,8/22/2016,10/30/2016
6,B,4,10/31/2016,12/25/2016
6,B,4,12/26/2016,1/8/2017
6,B,4,1/9/2017,5/7/2017
6,B,4,5/8/2017,6/11/2017
6,B,4,6/12/2017,6/25/2017
6,B,4,6/26/2017,12/31/9999
7,B,4,1/2/2012,12/30/2012
7,B,4,12/31/2012,12/29/2013
7,B,4,12/30/2013,5/18/2014
7,B,4,5/19/2014,11/16/2014
7,B,4,11/17/2014,12/28/2014
7,B,4,12/29/2014,3/8/2015
7,B,4,3/9/2015,1/18/2016
7,B,4,1/19/2016,2/19/2016
8,B,6,12/31/2012,11/3/2013
8,B,4,11/4/2013,12/29/2013
8,B,4,12/30/2013,1/26/2014
8,B,4,1/27/2014,5/18/2014
8,B,4,5/19/2014,11/16/2014
8,B,4,11/17/2014,12/28/2014
8,B,4,12/29/2014,3/22/2015
8,B,4,3/23/2015,12/27/2015
8,B,4,12/28/2015,7/1/2016
10,B,4,10/3/2011,12/18/2011
10,B,4,12/19/2011,12/30/2012
10,B,4,12/31/2012,2/23/2013
10,B,4,2/24/2013,11/20/2014
11,B,4,2/1/2011,2/27/2011
11,B,4,2/28/2011,5/1/2011
12,B,4,9/15/2012,12/31/2012
12,B,4,9/15/2012,12/31/2012
12,B,4,1/1/2013,12/31/2013
12,B,4,1/1/2013,4/30/2014
12,B,4,1/1/2014,4/30/2014
12,B,4,5/1/2014,11/16/2014
12,B,4,5/1/2014,12/28/2014
12,B,4,11/17/2014,11/30/2014
12,B,4,12/1/2014,12/28/2014
12,B,4,12/29/2014,12/27/2015
12,B,4,12/29/2014,12/30/2016
12,B,4,12/28/2015,12/30/2016
12,B,4,12/31/2016,12/31/2016
12,B,4,1/1/2017,6/11/2017
12,B,4,6/12/2017,6/25/2017
12,B,4,6/26/2017,7/9/2017
12,B,19,7/10/2017,12/31/9999
13,B,4,12/28/2015,9/4/2016
13,B,4,9/5/2016,12/25/2016
13,B,4,12/26/2016,6/11/2017
13,B,4,6/12/2017,6/25/2017
13,B,4,6/26/2017,12/31/9999
14,B,4,1/12/2015,12/27/2015
14,B,4,12/28/2015,12/25/2016
14,B,4,12/26/2016,6/11/2017
14,B,4,6/12/2017,6/25/2017
14,B,4,6/26/2017,12/31/9999
16,B,4,9/14/2015,10/19/2015
17,B,6,8/22/2016,12/25/2016
17,B,6,12/26/2016,5/7/2017
17,B,4,5/8/2017,6/11/2017
17,B,4,6/12/2017,6/25/2017
17,B,4,6/26/2017,7/9/2017
17,B,19,7/10/2017,12/31/9999
18,B,6,9/12/2016,12/25/2016
18,B,6,12/26/2016,5/7/2017
18,B,13,5/8/2017,6/11/2017
18,B,13,6/12/2017,6/25/2017
18,B,13,6/26/2017,7/9/2017
18,B,19,7/10/2017,12/31/9999
19,B,4,7/10/2017,12/31/9999
Empl ID is a unique employee number. Mgr ID references the Empl ID of the employee's manager at the desired point in time (Measure.LastMonth). Emp Attr is an attribute of the employee, such as level.
Does anyone have any ideas on how to create a measure that will count the occurrences of Empl ID in Mgr ID? Ideally, if I am creating a visual in Power BI and I filter based on Empl Attr ="A", can the resulting measure value give me the result = 3 -> the empl id "1" occurs 3 times in the mgr id column.
I need this to be a measure and not a calculated column so that I can trend the results over time (end of month on X axis in trend visual).
Thanks for the help and let me know if you have any questions!
Edit - Updating basically the entire answer due to new information about the problem.
I feel that your core problem is trying to create a relationship between two tables based on the evaluation of an expression (End of Month between Start Date and End Date). From my experience, the easiest way to workaround this is to CROSSJOIN the two tables and then filter it down based on whatever expression you would like. For this, I created a new table with this formula.
Results = DISTINCT(
SELECTCOLUMNS(
FILTER(
CROSSJOIN('Data Table', EOM),
'Data Table'[Start Date] <= EOM[End of Month] && 'Data Table'[End Date] >= EOM[End of Month]
),
"Emp ID", [Emp ID],
"End of Month", [End of Month]
)
)
One more piece before finally making the relationships, we need a list of unique employee IDs. That can easily be obtained by creating a new table with this formula.
Employees = DISTINCT(
SELECTCOLUMNS('Data Table',
"Emp ID", 'Data Table'[Emp ID]
)
)
From here, create relationships between all of the tables as shown in the image below.
I know you asked for a measure, but bear with me as I feel confident that this will get you what you want. Add a new column to the Results table with this formula.
DirectReports = CALCULATE(
COUNTROWS('Data Table'),
FILTER(ALL('Data Table'),
'Data Table'[Mgr ID] = EARLIER(Results[Emp ID]) &&
'Data Table'[Start Date] <= EARLIER(Results[End of Month]) &&
'Data Table'[End Date] >= EARLIER(Results[End of Month])
)
)
At this point, I would hide the Emp ID and End of Month from the results table and any other field(s) you desire. From there, make your visuals. For example, you said you wanted to show direct report count over time, so I made this simple line chart and card.

how to I define measurement filter from other table in tabular

I am trying to make measure that gets sum of amt column where versiontype column = ative how do I write dax formula for that measure?
So let's say your fact table is called Sales. It has a Sales Amount called Amt. The table is linked to a Version table with a VersionType field that includes a value of "Active".
You could create a new measure for Active Sales as follows:
ActiveSales :=
CALCULATE (
SUM ( Sales[Amt] ),
'Version'[VersionType] = "Active"
)

Resources