Forum Discussion
Choosing a column from a table using slicer
- 9 years ago
I think I know what the problem is!
The matrix (and also stacked bar chart or stacked column chart) persistantly shows me the values for TODAY, no matter which combination of slicers I make. This indicates that slicers have no effect on my measure...
The conditions (IFs) work fine, I have tested them... they react to slicer choices. Something is wrong with the functions I use (COUNTX, and I even tried with SUMX and then making a new measure as COUNTX('Calculated Experience'; [SelectedCalculation]) )
Any ideas?
The second part of the problem is to show the appropriate column in the matrix as the user chooses different values in slicers (one of the slicers is calculation choice with ability to select multiple calculations, and the other slicer chooses years, quartals, months, weeks...)
One of the suggestions I considered for this is to UNPIVOT all the dynamically created columns and then filter by chosen value in the new column (with calculation numbers). The code in Advanced Editor that appends on previous is:
#"Final Table" = AddColumnsFromList(#"Source 1 Final", ListOfNames),
// --- start append ----- (don't forget the comma on the end of the previous line)
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Final Table", {"Employee_No", "Years_of_Service", "Months_of_Service", "Days_of_Service", "Employment_Date", "Qualification"}, "Calculation No", "Total Years of Service"),
#"Changed Type" = Table.TransformColumnTypes(#"Unpivoted Other Columns",{{"Total Years of Service", Int64.Type}})
in
#"Changed Type"
The trick is to select all the columns I wish to keep and then unpivot ALL THE OTHERS (that way, I unpivot all of them, as many of them there are => as many calculations there are).
Now I have to make a measure to use on my matrix, which will satisfy all the conditions mentioned in the goal of my first post:
SelectedCalculation = IF( ISCROSSFILTERED('Calculations'[No]);
// If some calculations are selected by slicer selections
IF( HASONEVALUE('Calculations'[No]);
// If only one calculation is selected, put that calculation number in text (with function CONCATENATEX),
// filter the table on that criterion, and finally - count the rows
COUNTX(FILTER('Calculated Experience';
'Calculated Experience'[Calculation No] = CONCATENATEX(VALUES('Calculations'[No]);
'Calculations'[No];
", ")
);
'Calculated Experience'[Total Years of Service]
);
// ELSE, if multiple calculations are selected, take the last of them, put it in a text (with func. CONCATENATEX),
// filter the table on that criterion, and count the rows
COUNTX(FILTER('Calculated Experience';
'Calculated Experience'[Calculation No] = CONCATENATEX(LASTNONBLANK(VALUES('Calculations'[No]);
true);
'Calculations'[No];
", ");
);
'Calculated Experience'[Total Years of Service]
)
);
// ELSE (if no calculations were selected by slicers - take "Today")
COUNTX(FILTER('Calculated Experience';
[Calculation No] = "Today"
);
[Total Years of Service]
)
)
AAAAAAND, there is a bug somewhere. How do I know? Well, I made a test:
The first worker (E00001) was employed in 2006 and should have 10 years of experience on EACH calculation, but 11 years on TODAY.
I made one calculated column for reference
( ReferenceColumn = MAX('Calculated Experience'[Total Years of Service]; 0) )
and put it in a matrix:
When I use my measure in a matrix, for Qualification "II" I always get 22 employees in category "6 - 10 years" and 1 employee in "11 - 15 years", and that should be the case ONLY when no selection with slicers has been made. With each selection of slicers, there should be 23 employees in category "6-10 years". I am not able to find this bug, and whether the problem lies in my DAX code or in the PowerBI internal Matrix visualisation.
This solution (with UNPIVOTING) is very inefficient, because it makes HUGE tables (1000 employees TIMES 30 calculations - already makes considerable amount of records). I would prefer a solution which deals with the table prior to unpivoting, if possible (I have a feeling that this is not possible with DAX)
I think I know what the problem is!
The matrix (and also stacked bar chart or stacked column chart) persistantly shows me the values for TODAY, no matter which combination of slicers I make. This indicates that slicers have no effect on my measure...
The conditions (IFs) work fine, I have tested them... they react to slicer choices. Something is wrong with the functions I use (COUNTX, and I even tried with SUMX and then making a new measure as COUNTX('Calculated Experience'; [SelectedCalculation]) )
Any ideas?
- ivicaSD9 years agoFrequent Visitor
There is one good advice, found on site http://www.daxformatter.com/ in comments:
I will refactor my code now and see if it helps! :)
- ivicaSD9 years agoFrequent Visitor
Unfortunately, refactoring the code didn't solve the issue. I still get the results for "Today" and they don't change when I change various slicers... At least, I know that my code is more efficient now... ;)