Power BI - Parent Child Visualization - powerbi

I would like to perform following visualization.
Following is the table.
+---------------+-------------+---------------+-------------+--------+-------------+-----------+--+
| Customer Name | Customer ID | Location Name | Location ID | Rating | Parent Name | Parent ID | |
+---------------+-------------+---------------+-------------+--------+-------------+-----------+--+
| Customer 1 | 10 | Location 1 | L1 | 23 | Null | Null | |
| Customer 1 | 10 | Location 2 | L2 | 43 | Null | Null | |
| Customer 2 | 20 | Location 21 | L3 | 42 | Customer 1 | 10 | |
| Customer 2 | 20 | Location 22 | L4 | 54 | Customer 1 | 10 | |
| Customer 3 | 30 | Location 31 | L5 | 65 | Customer 1 | 10 | |
+---------------+-------------+---------------+-------------+--------+-------------+-----------+--+
Output should include Location Name and Rating.
+---------------+--------+--+
| Location Name | Rating | |
+---------------+--------+--+
| Location 1 | 23 | |
| Location 2 | 43 | |
+---------------+--------+--+
It has 2 filter Customer Name and Include Child Customer. Include Child Customer has 2 options "Yes" and "No".
If user selects "Yes" option then report should include locations of child customer.
As I am new to Power BI, I am not sure how to model data or achieve this functionality.
Sorry for all formatting issues.
Thanks.

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

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/

Django : Count, Group By and Order By with many to many fied

I have two models :
class Actors(models.Model):
name = models.CharField(max_length=128)
...
class Movies(models.Model)
title = models.CharField(max_length=128)
casting = models.ManyToManyField("actors.Actors", related_name="casting")
...
I am looking for ordering actors by using the number of movies they played in.
Django autogenerated a table with actors_id and movies_id, named movies_movies_casting :
+-----+-----------+-----------+
| id | movies_id | actors_id |
+-----+-----------+-----------+
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 2 | 1 |
| 4 | 3 | 1 |
...
Here actor #1 played in movies #1, #2 and #3. And actor #2 played only in movie #1.
Here is the request used in MySQL to get what I want :
SELECT actors_id, COUNT(movies_id) as count FROM movies_movies_casting GROUP BY actors_id ORDER BY count;
The result is :
+-----------+-------+
| actors_id | count |
+-----------+-------+
| 15 | 29 | // actor #15 played in 29 movies
| 12 | 21 |
| 24 | 17 |
| 3 | 16 |
| 20 | 15 |
| 21 | 14 |
...
What is the request used in Django to get this result ?
Try this,
from django.db.models import Count
Movies.objects.values('casting'
).annotate(
count=Count('id')).order_by('-count')

django Queryset exclude() multiple data

i have database scheme like this.
# periode
+------+--------------+--------------+
| id | from | to |
+------+--------------+--------------+
| 1 | 2018-04-12 | 2018-05-11 |
| 2 | 2018-05-12 | 2018-06-11 |
+------+--------------+--------------+
# foo
+------+---------+
| id | name |
+------+---------+
| 1 | John |
| 2 | Doe |
| 3 | Trodi |
| 4 | son |
| 5 | Alex |
+------+---------+
#bar
+------+---------------+--------------+
| id | employee_id | periode_id |
+------+---------------+--------------+
| 1 | 1 |1 |
| 2 | 2 |1 |
| 3 | 1 |2 |
| 4 | 3 |1 |
+------+---------------+--------------+
I need to show employee that not in salary.
for now I do like this
queryset=Bar.objects.all().filter(periode_id=1)
result=Foo.objects.exclude(id=queryset)
but its fail, how do filter employee list not in salary?...
Well here you basically want the foos such that there is no period_id=1 in the Bar table.
We can let this work with:
ex = Bar.objects.all().filter(periode_id=1).values_list('employee_id', flat=True)
result=Foo.objects.exclude(id__in=ex)

How to write a Django query where coloum_value < 100?

I have a table like this in DJango?
| id | user_id | name | source | remaining | start_date | time_remaining | size |
+----+---------+---------------+----------------+-----------+----------------------------+----------------+------+
| 1 | 1 | ok.txt | ngs.pradhi.com | 20 | February 05, 2013, 08:01AM | 1 | 4 MB |
| 2 | 1 | NC_008253.fna | ngs.pradhi.com | 20 | February 05, 2013, 08:02AM | 1 | 4 MB |
| 3 | 1 | test.data | ngs.pradhi.com | 0 | February 05, 2013, 08:21AM | 1 | 4 MB
I want to retrieve the data where user_id = request.user.id and remaining < 100.
Tried using:
Queue.objects.filter(user_id=request.user.id, remaining < 100) But didn't work.
Queue.objects.filter(user_id=request.user.id, remaining__lt=100).exclude(remaining=0)
Django Field lookups