Forum Discussion
Dual relationship and filtering
- 5 years ago
OK, I hope you're ready for this!
We're going to create this model:
Each of these tables are related on [commPatternCode].
dimCommsBridge is just there to avoid MANY:MANY relationships.
Everything we do will be in Power Query initially, with just one DAX measure at the end.***Preparing your fact table (I will refer to this as factComms):
-- I am assuming that your flag table only has one row per PC (but may have duplicated flag names).
1) Merge your flag table onto your fact table on flagTable[PC] = factComms[origination], expand Flag field and change the name to flagOrig.
2) Do the same on flagTable[PC] to factComms[destination], expand and rename to flagDest.
-- You should now have two new columns in your fact table that give you the flag names of the PC's involved in each row.
3) Create a new column in factComms by merging [flagOrig] and [flagDest] together using '-' as the delimiter.-- You should now have a new column in factComms that looks something like this in each row: Office-Lab. Call this [commPatternCode].
***Creating dimComms:
4) Create a blank query and, in the formula bar, type this:= Table.Distinct(Table.SelectColumns(factComms, {"flagOrig", "flagDest"}))to give you a table with all unique combinations of orig/dest from factComms.
5) Filter out nulls from both columns if necessary.
6) Add a new [commPatternCode] column in dimComms by merging your two columns together as you did before.
7) Add another new column in dimComms called [commSearchTerm], using this code:{[flagOrig], [flagDest]}-- Note the use of curly braces here!
😎 Expand this column TO NEW ROWS.
9) Remove any other columns keeping only [commPatternCode] and [commSearchTerm] and change data types to text.***Creating dimCommsBridge
10) Create a blank query and type this in the formula bar:Table.Distinct(Table.SelectColumns(dimComms, "commPatternCode"))11) Apply these new tables to your model and relate as per my initial screenshot.
-- Take care to notice that dimComms > dimCommsBridge filters in BOTH directions.
12) Now create a measure in your factComms table like this:
_countFilter = COUNTROWS(factComms)-- Relate any other dimension you want directly to the fact table, use dimComms[commSearchTerm] in your single-word slicer, and add the [_countFilter] measure as visual-level filters to any slicers etc. that you want to react to filter changes, with the logic [_countFilter] > 0.
Voila!
Pete
The flag table has this form
PC Flag
PC1 Lab
PC3 Lab
PC1 Office (Note this is valid two flags for same item)
PC7 Office
etc
I would need to adjust your suggestion?
Basically, for this part of the measure:
SELECTEDVALUE(flagTable[flagName])
you need a list of all the possible PC names (Lab, Office etc.) in a column.
What the measure's doing is checking whether there is a valid PC name in the list that matches any part of the text in your concatenated column. If there is, it outputs a number which is the character position in the concatenated text of where the PC name is found. That's why the filter logic is [_measure] >= 1. If there's a valid PC name in the concatenated text, the measure will ouput a position number that will be > 0, so the filter keeps it in the output.
Pete
- arcegabriel5 years ago
Helper I
BA_Pete Thanks
I understand the concept but not sure I understand how to pull it all together
Ultimately, I want to have on the visualization a filter called see all the options (generated from the data) and checkbox next to them
[] Lab
[] Office
[] Den
When I click on "Lab" I would like the data to be filtered in the manner I described (i.e. filter out anything that does not have "Lab" in the communications table as either/both origin or destination)
- BA_Pete5 years ago
Super User
OK, so it sounds like we need a unique flag name list to get this to work.
In Power Query, create a new blank query and type this into the formula bar:
= Table.Distinct(Table.SelectColumns(theNameOfYourFlagTableQuery, "Flag"))This should create you a distinct list of all the possible names from your flags table. Filter out any null values. Call this new query 'dimFlagName'.
Then update the measure as follows:
flagNameExists = SEARCH( SELECTEDVALUE(dimFlagName[Flag]), //I've updated this to reference the new distinct table SELECTEDVALUE(commsTable[pcConc]), -1 )You would use this new dimFlagName[Flag] field in your report slicer.
Then follow the other steps as previously provided.
If this still doesn't work for you, then you may need to provide some of your actual data and/or your PBIX file so I can see what's going on with the actual data/model.
Pete
- arcegabriel5 years ago
Helper I
BA_Pete Brilliant. Got it. Sorry for slowness just getting started with power bi. I did notice one small error on your formula (missing a comma - should be two before -1)
flagNameExists = SEARCH( SELECTEDVALUE(dimFlagName[Flag]), //I've updated this to reference the new distinct table SELECTEDVALUE(commsTable[pcConc]),, -1 )Really appreciate it