Calculate totals within custom SQL query (Tableau) - if-statement

I have a dataset where I wish to reflect the totals from a custom SQL query I performed in Tableau. Here is some sample data:
1. I first performed a custom query that was a join, unpivot and placed my data into groups
Size Tb Val type Group Sum_AVG SKU Last_Refreshed
270 90.5 Free_Space_TB Group2 90.5 Excel 9/1/2020
270 179.5 Used Group2 179.5 Excel 9/1/2020
814 701 Free_Space_TB Group1 701 Gris 8/1/2020
814 112 Used Group1 112 Gris 8/1/2020
2. Then I aggregated the data by taking the sum of one group and the average of the other group (and final summed these groups values)
The data is being aggregated like this: (SUM_AVG)
zn(sum(if [Group]= 'Group1' then [Val] end))
+
zn(avg(if [Group] = 'Group2' then [Val] end))
The view looks like this
Here is the custom query output
Here is my view
The avail and used appear when I hover over, but how would I include the total?
This is the calculation I am using (thanks to help from a SO member):
{SUM({Fixed [type]: ZN(sum(if [Group]= 'Group1' then [Val] end))})
+
sum({Fixed [type]: zn(avg(if [Group] = 'Group2' then [Val] end))})}
I am doing something wrong, because it is totaling up across all the column(s), (I have more columns in the full dataset) when I just want the total for each column.
(Used was created from using a custom query)
Any assistance is appreciated.

In my opinion, this you can do without changing the underlying view. WINDOW_SUM is a table calculation and is always dependent on view/context generated. Therefore, I always prefer LOD calculations which do not depend on context.
I think you should proceed like this. As always I have changed the sample data to include sufficient details
Data used
| Id | Avail | group | used | Date |
|----|-------|--------|------|------------|
| A | 5 | Group1 | 5 | 20-01-2020 |
| A | 20 | Group1 | 20 | 20-01-2020 |
| B | 10 | Group2 | 10 | 20-01-2020 |
| B | 5 | Group2 | 5 | 20-01-2020 |
| B | 5 | Group2 | 5 | 20-01-2020 |
| A | 10 | Group1 | 10 | 20-01-2020 |
| A | 10 | Group1 | 10 | 20-01-2020 |
| B | 5 | Group2 | 5 | 20-01-2020 |
| B | 5 | Group2 | 5 | 20-01-2020 |
| A | 5 | Group1 | 5 | 20-02-2019 |
| A | 20 | Group1 | 20 | 20-02-2019 |
| B | 10 | Group2 | 10 | 20-02-2019 |
| B | 5 | Group2 | 5 | 20-02-2019 |
| B | 5 | Group2 | 5 | 20-02-2019 |
| A | 10 | Group1 | 10 | 20-02-2019 |
| A | 10 | Group1 | 10 | 20-02-2019 |
| B | 5 | Group2 | 5 | 20-02-2019 |
| B | 5 | Group2 | 5 | 20-02-2019 |
Step-1 Pivot generated in tableau as earlier.
Step-2 Calculated field sum-avg also generated as discussed.
step-3 View generated
Step-4 Add another field total
{FIXED [Date], [Group]: sum(
{FIXED [Date], [Group], [type]: zn(sum(if [Group]= 'Group1' then [val] end))}
+
{Fixed [Date], [Group], [type]: zn(avg(if [Group] = 'Group2' then [val] end))}
)}
Step-5 Add this field to details on marks card. See the GIF here
the code used in tooltip is mentioned below. Obviously, you can tweak it as per taste.
Under the <Group> , <AGG(Sum_Avg)> was <type> out of total <SUM(Total)> SKU on <YEAR(Date)>

This solution works:
1.Create a calculated field:
WINDOW_SUM([SUM_AVG])
2.Drag newly computed field to the view
3.Right click ‘Edit Table Calculation’
4.Specify and compute using [Last_Refreshed] and [type]
This will allow you to compute across cells, giving you your desired result

Related

How to find count of Direct Reporting's by DAX formula in Power BI?

Good day! I have a sample employee table like the one below. I need a DAX formula in Power BI to create a measure to count the number of direct reports of each employee. For Example, the Direct Report count of GL0001 will be 2 (Because GL0001 is the line manager of GL0002 and GL0019 and they report to GL0001), the Direct Report count of EMP-02023 will be 3, Direct Report count of GL0002 will be 3. Please help me also to create measures regarding the count of only one direct reporting and less than three direct reporting
| Employee ID | Line Manager ID | Layer (of Employee) | Layer (of Line Manager) |
|--------------|-------------------|----------------------|--------------------------|
| EMP-01980 | GL0003 | 4 | 3 |
| EMP-02023 | EMP-02015 | 6 | 5 |
| EMP-01636 | EMP-02015 | 6 | 5 |
| EMP-02138 | EMP-02162 | 6 | 5 |
| EMP-02145 | EMP-01980 | 5 | 4 |
| GL0023 | GL0022 | 5 | 4 |
| GL0001 | | 1 | 0 |
| GL0002 | GL0001 | 2 | 1 |
| GL0003 | GL0002 | 3 | 2 |
| GL0019 | GL0001 | 2 | 1 |
| GL0020 | GL0002 | 3 | 2 |
| GL0024 | GL0002 | 3 | 2 |
| EMP-01918 | EMP-00791 | 9 | 8 |
| EMP-01941 | EMP-00791 | 9 | 8 |
| EMP-02019 | EMP-02156 | 8 | 7 |
| EMP-02024 | EMP-02023 | 7 | 6 |
| EMP-02025 | EMP-02023 | 7 | 6 |
| EMP-03001 | EMP-02023 | 7 | 6 |
Your data doesn't have all the Employee ID for each Line Manager ID. That means the PATH calculation would not work.
I've assumed your data looks like this
Employee ID
Line Manager ID
1000001
1000002
1000001
1000003
1000002
1000004
1000003
1000005
1000004
1000006
1000005
1000007
1000006
1000008
1000007
1000009
1000006
1000010
1000003
Creating Calculated columns you can calculate the PATH and the PATH SIZE
Path
Path = path('Table'[Employee ID],'Table'[Line Manager ID])
Path Size
Path Length = PATHLENGTH([Path])
Output
Edit
In that case, you can use the Line Manager ID column to count direct reports, measure below.
DAX: Calculated Column
CountDirectReport =
VAR EmpId = [Employee ID]
RETURN
CALCULATE (
COUNTROWS ( 'Table' ),
FILTER ( 'Table', [Line Manager ID] = EmpId )
)
Output

PowerBI : Count Distinct values in one column based on Distinct Values in another column

i have a data for group no and its set value. When the set value is same for all the batches i dont want those batches to be counted. but if there are more than 1 set values in each batch then the dax query should count it as 1.
My current data is like this
| group_no | values |
| ---------- | ---------------------- |
| H110201208 | 600 |
| H110201208 | 600 |
| H110201208 | 680 |
| H101201215 | 665 |
| H109201210 | 640 |
| H123201205 | 600 |
| H125201208 | 610 |
| H111201212 | 610 |
| H111201212 | 630 |
I want my output like this
| Group no | Grand Total |
| ---------- | ----------- |
| H101201215 | 1 |
| H109201210 | 1 |
| H110201208 | 3 |
| H111201212 | 2 |
| H123201205 | 1 |
| H125201208 | 1 |
i want to create another table like the one above using dax so that i can plot graphs n percentages based on its output
i want to do this in powerbi using DAX language.
TABLE =
GROUPBY (
Groups, //SourceTable
Groups[ group_no ],
"GrandTotal", COUNTX ( CURRENTGROUP (), DISTINCTCOUNTNOBLANK ( Groups[ values ] ) )
)

Power Bi, Dax - calculations, filters, balance

Could you please help me to solve the problem as I am totally new to DAX and English is not my first language so I am struggling to even find the correct question.
Here's the problem.
I have two tables:
start_balance
+------+---------------+
| Type | Start balance |
+------+---------------+
| A | 0 |
| B | 10 |
+------+---------------+
in_out
+------+-------+------+----+-----+
| Year | Month | Type | In | Out |
+------+-------+------+----+-----+
| 2020 | 1 | A | 20 | 20 |
| 2020 | 1 | A | 0 | 10 |
| 2020 | 2 | B | 20 | 0 |
| 2020 | 2 | B | 20 | 10 |
+------+-------+------+----+-----+
I'd like to get the result as follows:
Unfiltered:
+------+-------+------+---------+----+-----+------+
| Year | Month | Type | Balance | In | Out | Left |
+------+-------+------+---------+----+-----+------+
| 2020 | 1 | A | 0 | 20 | 20 | 0 |
| 2020 | 1 | B | 10 | 20 | 10 | 20 |
| 2020 | 2 | A | 0 | 20 | 10 | 10 |
| 2020 | 2 | B | 20 | 20 | 10 | 30 |
+------+-------+------+---------+----+-----+------+
Filtered (for example year/month 2020/2):
+------+-------+------+---------+----+-----+------+
| Year | Month | Type | Balance | In | Out | Left |
+------+-------+------+---------+----+-----+------+
| 2020 | 2 | A | 0 | 20 | 10 | 10 |
| 2020 | 2 | B | 20 | 20 | 10 | 30 |
+------+-------+------+---------+----+-----+------+
So while selecting a slicer for the year/month it should calculate balance before selected year/month and then show selected year/month values.
Edit: corrected start_balance table.
Is the sample data correct?
A -> the starting balance is 10, but in your unfiltered table example, it is 0.
Do you have any relationship between these tables?
Does opening balance always apply to the current year? What if 2021 appears in the in_out table? How do you know when the start balance started?
example without starting balance
If you want to show value breaking given filter you should use statement ALL or REMOVEFILTERS function (in Analysis Services 2019 and in Power BI since October 2019).
calculate(sum([in]) - sum([out]), all('in_out'[Year],'in_out'[Month]))
More helpful information:
https://www.sqlbi.com/articles/managing-all-functions-in-dax-all-allselected-allnoblankrow-allexcept/

Power BI custom column

We have some issues in removing some unwanted data from a large dataset.
The data set looks similar to the table below:
Inv_Number | Type | Week | Quarter | Amount | Order
1 | Invoice | W1 | Q1 | 100 | A1233
2 | Invoice | W2 | Q1 | 50 | A100
3 | Invoice | W2 | Q1 | 150 | A567
4 | CR MEMO | W3 | Q2 | -100 | A1233
5 | Invoice | W2 | Q4 | 70 | A345
6 | Invoice | W5 | Q3 | 100 | A1233
7 | CR MEMO | W7 | Q2 | -25 | A100
The expected filtered result should look like:
Type | Week | Quarter | Amount | Order
Invoice | W2 | Q1 | 25 | A100
Invoice | W2 | Q1 | 150 | A567
Invoice | W2 | Q4 | 70 | A345
Invoice | W5 | Q3 | 100 | A1233
Basically, we have a unique identifier (Order) and we need to remove all CR MEMO and related Invoices (partial or not)
I have tried the following:
HasCredit = if(CALCULATE(SUM('inv'[Amount]),FILTER(ALL('inv'),inv[Order]=EARLIER(inv[Order]) && inv[Type]="CR MEMO"))+CALCULATE(SUM(inv[Amount]),FILTER(ALL('inv'),inv[Order]=EARLIER(inv[Order])&&inv[Type]="ORIGINAL"))=0,1,0)
Then add the new calculated field to the filter and select only 0 (zero) should provide the desired output
Further explanations:
The data set speaks about invoices and CR MEMO can be understand as credited invoices. If you have one invoice (ex: Inv_number = 1) with 100USD credited entirely, you will have a new invoice (ex: Inv_number = 4) with Type = CR MEMO. Now, I need to remove those lines that are only linked by the Order and present the final output as already described. Keep in mind that one CR MEMO (credited invoice) can have a partial amount from one original invoice

Django calculate percentages within group by

I have a model for which I want to perform a group-by on two values and calculate the percentages of each value per outer grouping.
Currently I just make a query to get all the rows and put them into a pandas dataframe and perform something similar to the answer here. Although this works I'm sure it would be more efficient if I could make the query return the information I require directly.
I am currently running Django 2.0.5 with a backend DB on PostgreSQL 9.6.8
I think window functions could be the solution as indicated here but I cannot construct a successful combination of annotate and values to give me the desired output.
Another possible solution could be rollup introduced in PostgreSQL 9.5 if I can find a way to get the summary row as a set of extra columns for each row? But I also think it's not yet supported by Django.
Model:
class ModelA(models.Model):
grouper1 = models.CharField()
grouper2 = models.CharField()
metric1 = models.IntegerField()
All rows:
grouper1 | grouper2 | metric1
---------+----------+---------
A | C | 2
A | C | 2
A | C | 2
A | D | 4
A | D | 4
A | D | 4
B | C | 5
B | C | 5
B | C | 5
B | D | 6
B | D | 4
B | D | 5
Desired output:
grouper1 | grouper2 | sum(metric1) | Percentage
---------+----------+--------------+-----------
A | C | 6 | 40
A | D | 12 | 60
B | C | 15 | 50
B | D | 15 | 50
I got close to what I expected with
ModelA.objects.all(
).values(
'grouper1',
'grouper2'
).annotate(
SumMetric1=Window(expression=Sum('metric1'), partition_by=[F('grouper1'), F('grouper2')]),
GroupSumMetric1=Window(expression=Sum('metric1'), partition_by=[F('grouper1')])
)
However this returns a row for every original row in the database like so:
grouper1 | grouper2 | sum(metric1) | Percentage
---------+----------+--------------+-----------
A | C | 6 | 40
A | C | 6 | 40
A | C | 6 | 40
A | D | 12 | 60
A | D | 12 | 60
A | D | 12 | 60
B | C | 15 | 50
B | C | 15 | 50
B | C | 15 | 50
B | C | 15 | 50
B | C | 15 | 50
B | D | 15 | 50
In this situation .distinct() might help.
More information is here.