Slicer for Merged, Multi-Column Rows - powerbi

I have two (2) tables: Invoices and Products. They look like this:
**Invoices**
InvoiceID
SalesRep
**Products**
InvoiceID
ProductID
Cost
ProductName
The Invoices.InvoiceID column is unique. Any invoice may have 1-n products attached to it. In the Products table, there can be any number of duplicate InvoiceIDs, each showing one ProductID (and its cost) that appeared on that Invoice.
Our business sells licenses to use our products (not the products themselves; just licenses). Those licenses can be renewed yearly. So in our DB, our products have names like 'Product A' and 'Product A (Renewal)'. The '(Renewal)' bit indicates that this product is actually a renewal of Product A, not the first purchase of the license. Apart from that, they are identical.
We need to split our invoices and their products by Renewals and non-renewals (we call it New). In the output table, we want just one row for each invoice, no matter how many products it has. It'll look like this:
|Invoice ID|Sales Rep|Total Cost|Renewal Products |Renewal Cost|New Products |New Cost|
|001 |F. Smith | $100|Product A (Renewal)| $100 | | |
|002 |J. Blow | $250|Product B (Renewal)| $150 |Product A | $100|
|003 |B. Bloggs| $300| | |Product C & Product D| $300|
And so on. Note that where there are multiple products of either New or Renewal, they are concatenated. I achieved that by the DAX:
New Products = CONCATENATEX (filter(Products,
Products[InvoiceID]=min(Invoices[InvoiceID]) && iserror(SEARCH("renewal",
Products[ProductName]))),
Products[ProductName],
" & " ,
Products[ProductName],
ASC)
and I did a similar one for the Renewal products. The costs, too, add together, so that if there are multiple products of either New or Renewal, their costs add together, like for invoice 003 in the table above. Again, DAX:
New Total = SUMX(FILTER(Products,
Products[Invoice ID]=min(Invoices[InvoiceID]) && iserror(SEARCH("renewal", Products[ProductName]))),
Products[Cost])
So far, so good. I have the table looking the way I want it. But the business wants to be able to slice the output table by New or Renewal. When they choose either, then it as if the other type of business doesn't even exist. The columns for that type of business in the output table will be empty, and the Total Cost column will show only the total cost of any items of the chosen type in the order. And if any invoice has only products of the type not chosen, that invoice will disappear from the output completely.
I am really struggling with this slicer, so any suggestions will be gratefully received. Thanks a lot
Edit
Alexis, if the original table looks like this:
|Invoice ID|Sales Rep|Total Cost|Renewal Products |Renewal Cost|New Products |New Cost|
|001 |F. Smith | $100|Product A (Renewal)| $100 | | |
|002 |J. Blow | $250|Product B (Renewal)| $150 |Product A | $100|
|003 |B. Bloggs| $300| | |Product C & Product D| $300|
Then after slicing to show only New, it would look like this:
|Invoice ID|Sales Rep|Total Cost|Renewal Products |Renewal Cost|New Products |New Cost|
|002 |J. Blow | $100| | |Product A | $100|
|003 |B. Bloggs| $300| | |Product C & Product D| $300|
And after slicing to show only Renewal, it would look like this:
|Invoice ID|Sales Rep|Total Cost|Renewal Products |Renewal Cost|New Products |New Cost|
|001 |F. Smith | $100|Product A (Renewal)| $100 | | |
|002 |J. Blow | $150|Product B (Renewal)| $150 | | |
Does that help?

This is actually simpler than you might expect. All you need to do is create a calculated column on the Products table that distinguishes between the new and renewal rows:
Slicer = IF(SEARCH("Renewal", Products[ProductName], 1, 0) > 0, "Renewal", "New")
Then use this column as a slicer and the table should behave as you specified.
Note: If you create this column first, then your measures can reference it rather than using the SEARCH function. I.E., you can replace
`iserror(SEARCH("renewal", Products[ProductName]))`
with
Products[Slicer] = "New"

Related

Slicer controls two columns

I'm quite new to Power BI. I have one lookup table and multiple fact tables and their relationships are all one to many.
I want to create a slicer using category desc column from my lookup table. The slicer is going to filter both category desc and old category columns in a table. Also it is filtering other visuals as a normal filter.
For example if you select golf, the table is going to show category = golf or old category = golf.
Is it possible to just use the slicer from the lookup table which would act as a normal slicer (filtering category desc in my other visuals) while at the same time filter this specific table visual based on the criteria above? Appreciate anyone's help!
|Category Desc |Old Category |Type |Amount|
|Golf |Tennis |J |11 |
|Tennis |Golf |K |12 |
|Social |Fitness |J |44 |
|Fitness |Social |K |32 |
|Golf |Other |K |23 |
|Other |Social |J |26 |
If you want to filter only one specific table, then consider using a measure. Something like this.
PickThis =
var _FilterSelect = SELECTEDVALUE('LookupTable'[Label])
return
calculate( countrows('SpecificTable'), FILTER(ALL('SpecificTable'[Category], 'SpecificTable'[OldCategory]), 'SpecificTable'[OldCategory]= _FilterSelect || 'SpecificTable'[Category] = _FilterSelect ))

Split data into categories in the same row in Power BI

I have a table that contains multiple columns with their named having either the suffix _EXPECTED or _ACTUAL. For example, I'm looking at my sold items from my SoldItems Table and I have the following columns: APPLES_EXPECTED, BANANAS_EXPECTED, KIWIS_EXPECTED, APPLES_ACTUAL, BANANAS_ACTUAL, KIWIS_ACTUAL (The Identifier of the table is the date, so we have results per date). I want to show that data in a table form, something like this (for a selected date in filters:
+------------+----------+--------+
| Sold items | Expected | Actual |
+------------+----------+--------+
| Apples | 10 | 15 |
| Bananas | 8 | 5 |
| Kiwis | 2 | 1 |
+------------+----------+--------+
How can I manage something like this in Power BI ? I tried playing with the matrix/table visualization, however, I can't figure out a way to merge all the expected and actual columns together.
It looks like the easiest option for you would be to mould the data a bit differently using Power query. You can UNPIVOT your data so that all the expected and actual values become rows instead of columns. For example take the following sample:
Date Apples_Expected Apples_Actual
1/1/2019 1 2
Once you unpivot this it will become:
Date Fruit Count
1/1/2019 Apples_Expected 1
1/1/2019 Apples_Actual 2
Once you unpivot, it should be fairly straightforward to get the view you are looking for. The following link should walk you through the steps to unpivot:
https://support.office.com/en-us/article/unpivot-columns-power-query-0f7bad4b-9ea1-49c1-9d95-f588221c7098
Hope this helps.

PowerBI Desktop - SUM Ignoring Filters

My visual has a simple data table which has a number of rows, each representing an invoice. Each invoice has a number of items, including the sales rep, creation date, and the amount.
I have a number of slicers including sales rep, date of creation, etc.
I'm trying to add a column which, for each row, shows the percentage that that particular invoice is of the shown invoices. So, for example, when I use a filter to only show a subset of all rows, I want each row to show a percentage of the total of that subset, not the percentage of all rows.
Imagine this table:
|ID|Rep |Value|Percentage|
| 1|Dave| 10| 50|
| 2|Sue | 3| 15|
| 3|Dave| 5| 25|
| 4|Sue | 2| 10|
The Percentage column is created by Value/SUM(Value) and is showing correctly above. The SUM(Value) is 20; Dave's 10 is 50% of that, Sue's 3 is 15% of it, and so forth.
I have a slicer for Rep. So if I choose 'Dave' in that slicer, the table looks like this:
|ID|Rep |Value|Percentage|
| 1|Dave| 10| 50|
| 3|Dave| 5| 25|
The Percentage column is being created based on the SUM(Value) of ALL rows (i.e. 20) not just the ones that are showing because of my slicer (i.e. 15). I would have expected the percentages to be 66.67% and 33.33%, being the percentages of the total of what's showing. Why isn't it? And how do I get it to be?
It looks like your percentage column is a calculated column rather than a measure. Calculated columns cannot be responsive to slicers as they are calculated only once per data load.
Try creating Percentage as a measure instead:
Percentage = SUM(Table1[Value]) / CALCULATE( SUM(Table1[Value]), ALLSELECTED(Table1) )

How to capture slicer value by DAX measure

How to capture slicer value by DAX measure in all circumstances? Let's have sample data:
+-----------+---------+-------+
| category | species | units |
+-----------+---------+-------+
| fruit | apple | 1 |
| fruit | banana | 1 |
| vegetable | carrot | 1 |
| vegetable | potato | 1 |
+-----------+---------+-------+
I added two measures:
Measure 1:
species selected = SELECTEDVALUE(Table1[species])
Measure 2:
IsFiltered = ISFILTERED(Table1[species])
Case 1. All items in both slicers selected.
Case 2. (problematic case). Fruits selected and Carrots selected (it is possible when we untie slicers interactions).
In case when we select fruit category from one slicer and carrot from another slicer there is a problem. This set of items is obviously empty. However definitely carrot from species have been selected and it is confirmed by IsFiltered measure which evaluates to True. Is there a way to capture that value in DAX measure?
Since both the category and species slicers come from columns on the same table, if you have both fruit and carrot selected, then the resulting table is empty and any measures (except ones that remove both filters) will therefore be working with blanks. You cannot have both filters apply simultaneously an expect them to act independently (even if the two slicer visuals don't cross-filter).
If you don't want your species selected measure to be influenced by category, the simplest thing to do would be to turn off filtering (under Format > Edit interactions) from the category slicer to the visual containing species selected.
This isn't always what you want though, so another possibility is to create a new table for the species slicer which has no filtering relationship from Table1. This will allow you to work with the slicers selections separately if that's something you need to do. (I've definitely had to do this before when I wanted a slicer to behave more like a parameter than a filter.)
Edit: To do what I suggested, create a new Table2 in the query editor that references Table1, remove all columns other than species and remove duplicate if necessary. You should now have a single column table that is a list of unique species.
When you close and apply, Power BI will likely automatically create a relationship between the two tables, but you need to make sure it's exactly what you want. It needs to be a many to one relationship with a single filter direction.
Once this is done, you'll need to replace the Table1[species] slicer with Table2[species] slicer as well as change references in measures where necessary.

Better Design for a multi column search - Django PostgreSQL

I have a table of products(productsmaster).
For each product in the table, I have four columns to show the months in which quarterly check dates are conducted (product1: Q1-Jan, Q2-Apr; product2: Q1-Dec,Q2Mar...)
I am developing an app in Django-PostgreSQL that for a specified month, it picks all products that are reporting in the specified month ie that have the month in column Q1 or Q2 or Q3 or Q4.
The products are set up once but are accessed a lot more when being read for reports and processing. I am sorry I dont have any code yet as I am looking to clarify design before I start coding. Can anyone advise how I can improve this? Database design if possible but willing to use django solutions too.
You should have a dedicated model for this, call it ProductCheck
product | quater | month
------------------------
prod1 | Q1 | Jan
prod1 | Q2 | Apr
prod2 | Q1 | Dec
prod2 | Q2 | Mar
Then your query looks like this:
ProductCheck.objects.filter(month="Jan")
Remember to put indexes on the fields that are most commonly searched upon, to speed up the query. Also you can add unique constraint to avoid duplicated data.