Better Design for a multi column search - Django PostgreSQL - django

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.

Related

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.

Slicer for Merged, Multi-Column Rows

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"

Power BI Desktop Line Chart with Multiple Lines

I'm trying to overlay three different columns in one chart on Power BI Desktop.
To try to show a comparison of the number of orders per month by different years I have one column for Order Number, and three other columns for Sale Date; Sale Date 2016, Sale Date 2017 and Sale Date 2018. For each order number the three Sale Date columns have a number from 1-12 (representing the month in that year that the order occurred) or a null value (if the order occurred in another year). Something like this:
I'm trying to overlay the three Sale Date columns as individual lines on a line chart with Order Number (Count Distinct) as one axis and Sale Date 1-12 as the other axis. I would think that this would be comparatively simple but Power BI Desktop doesn't seem to have an option to set this up. I'd be open to other types of chart too, as long as the different years can be easily compared.
The reason you're having trouble is that your data is not shaped in a way that is conducive to the way Power BI works.
I'd recommend unpivoting your Sale 201x columns in the Query Editor before trying to do anything. When you do this, your data will look like this instead.
Order # | Year | Month
--------|-----------|------
101 | Sale 2016 | 6
102 | Sale 2017 | 8
103 | Sale 2018 | 12
104 | |
When it's in this format you can use it in a visual much more easily using Order # on your axis and Month for values while using Year as your legend.

Power BI - how to count duplicates from two identical sets with different filters

I have two tables which contain the same data - TableA (PersonId, ItemId, Session) and TableB(PersonId, ItemId, Session). Here is some test data:
PersonId | ItemId | Session
-------------------------------------------------
1 | 1 | Summer 2017
1 | 1 | Spring 2017
2 | 1 | Summer 2017
2 | 2 | Summer 2017
3 | 5 | Spring 2017
There are two slicers for the different sessions (for simplicity I have only showed two in the test data but they may be a lot more). The user has to see the number of different PersonIds for the selected session in the first slicer by
ItemId. Then there should be information on the number of PersonIds that are both present in the first and second sessions in the selected slicers.
My idea is to filter TableA by the selected value in the first slicer and then filter TableB by the selected value in the second slicer, but I am not sure how to do that dynamically? Also, I may need a third table for the intersection of filtered TableA and Filtered TableB. Do you have any ideas on how I achieve this functionality?

Power BI Unable to select sort columns

I am creating a Month table to be included in my Power Bi Dashboard as a Filter.
I create a separate table joined to main table, but unable to select the option from the menu to Sort By Column- Greyed out.
What am I missing. I just want a filter of Months (Jan, Feb, Mar, etc) in numeric order. Can someone direct me to a good how to?
If a sort has already been specified, it will be greyed out.
Click on the Data icon in the upper left.
Click on the column that may have a sort on it.
Click Clear Sort.
Click on another column, then click back to the column you want to sort.
Sort By Column will no longer be greyed out.
When you create Report using Power bi data set (not connect directly to database) you cant use column sort it will appear disabled
Appending numbers to the beginning of your linked Month table might help you issue.
If your data looks something like this..
Revenue by month ID
Month table
Then make sure your month table is formtted like so in PBI
You can then filter like so
Alternatively, you could format the sorting month column to text and filter like so:
There's a cleaner way to do this. It's demonstrated starting at about 10:50 in this video.
https://youtu.be/d2bZpNZ6uIA?t=10m50s
You'll need to have a column of number values that correspond to the text values and then tell Power BI to sort by the number column. You can add such a column with "Enter Data" if it doesn't exist.
The preferred way to do this is to use a sort by column that looks like this:
Month | MonthSort
Jan | 1
Feb | 2
Mar | 3
Apr | 4
May | 5
Jun | 6
Jul | 7
Aug | 8
Sep | 9
Oct | 10
Nov | 11
Dec | 12
Then on the modeling tab, select the Month column. Then click the Sort By Column button in the ribbon and select MonthSort.
https://learn.microsoft.com/en-us/power-bi/desktop-sort-by-column#sort-using-the-sort-by-column-button
This is an old post, however, I was not able to solve based on above answers. So updating based on what helped me.
In Power query editor, create a new column in the same table as month number for corresponding month. Apply the changes and return to report screen.
In report screen, select the field "Month", go to modelling and click sort by "Month Number". Note:- this makes Month to be sorted based on Month number rather than alphabetical.