Distinct count of rows using a join in Power BI - powerbi

I want to get a distinct count of Enquiries with Food and Drink in Power Bi. The way the database is structured is:
1 Enquiry could have multiple days, which have food and drink attached to them.
I want to try and calculate how many enquiries have food and drink attached so I need to do a distinct count on the enquiry id in the Enquiries table where a record exists in the food and drink table
The table structure is as follows:
Enquiry Table
EnquiryId
10
Enquiry Day Table
EnquiryDayID EnquiryId
5 10
6 10
Enquiry Day Food Drink Table
EnquiryDayFoodDrinkId EnquiryDayId
20 5
21 6
Basically I want the distinct count to only return 1 as there is only one distinct enquiry with food + Drink attached.
Here is the code in SQL which return the correct number. I want to get the same result in Power BI
select count (distinct(e.pkEnquiries))
from EnquiryDayFoodDrink edfd
inner join EnquiryDay ed
on ed.EnquiryDayId = edfd.EnquiryDayId
inner join Enquiries e
on ed.EnquiryId = e.pkEnquiries

Related

PowerBI DAX Query - undo filter slicer on certain values based on expression

I have below table structure:
enter image description here
here I want to put a date slicer in Power BI to filter on dates and return the count of total rows as total in the card as shown below:
enter image description here
simple, the only twist is that I want to have the total of hybrid car added at all times.
i.e.
Brands before 5/25/2020 = 4 Hybrid + 1 Electric = 5
Brands before 12/5/2020 = 4 Hybrid + 3 Electric = 7
I have found a solution, which is creating a view in my database, which jus holds the number count of hybrid car (select count(*) from table where cartype = 'hybrid') and using it to sum with filter rows in power bi - but I am looking for a solution completely in Power BI DAX query.
any measure I have tried to create in power bi is filtered by date slicer and so doesn't work.
Create these measure:
TOTALROWS = COUNT('cars'[brand])
ELECTRIC_NUM = CALCULATE([TotalRows],('cars'),'cars'[cartype]="ELECTRIC")
HYBRID_NUM = CALCULATE([TOTALROWS],ALL('cars'),'cars'[cartype]="HYBRID")
TOTALBYBUSINESSLOGIC = CALCULATE([ELECTRIC_NUM]+[HYBRID_NUM])
Now use the last measure (i.e. TOTALBYBUSINESSLOGIC) to be used in your Card to display the total, Notice the expression diffrence between ELECTRIC_NUM and HYBRID_NUM
(In HYBRID_NUM I have used ALL, All will have it bypass the Date Slicer filter) whereas ELECTRIC_NUM will only proivde sum of rows falling in the active date sliver range.

No data in related table - artificial blank category DAX

I have following questions.
Table Sales:
Table Product:
Table Sales is connected to table Product via Sales ID.
Table Sales contain 5 unique records, table Product contain only 3 of them. 2 are missing. Is there some way how to artificially create in Power BI missing category?

How to group by on Power BI using DAX

I have two tables in Power BI as follows:
COUNTRIES
COD COUNTRY
1 BRAZIL
2 ARGENTINA
3 CHILE
4 BRASIL
5 COLOMBIA
6 ARGENTINA
7 URUGUAI
SALES
COD DATE
1 2021-01-02
2 2021-10-01
3 2019-09-04
1 2018-07-05
7 2019-04-10
There's a relationship between the two tables, on the COD column.
I need to count how many countries (column "COUNTRY" from the table "COUNTRIES") have a status CHURN. It's considered CHURN when their latest order from the table "SALES" is more than 180 days, using today as a reference.
I know that I need to group by the MAX date per country, do a DATEDIFF, and then do a COUNT. I've tried using ALL and SUMMARIZE, but I haven't found a way to solve this problem.
Are you able to add a calculated column to store the max sales date for each country in your COUNTRIES table? Either in Power BI or directly in your database. If so, here's one solution with 2 steps.
Create a MaxSalesDate column in your COUNTRIES table. DAX for a calculated column below:
MaxSalesDate =
VAR COD = COUNTRIES[COD]
RETURN MAXX(FILTER(SALES, SALES[COD] = COD), SALES[DATE])
Create a measure that counts the number of MaxSalesDate values that are older than 180 days old:
CountCHURN = COUNTX(COUNTRIES, IF(DATEDIFF(COUNTRIES[MaxSalesDate], TODAY(), Day) > 180, 1))

Power BI How to Sum Matched 2 Columns

I have two tables. First one represents sales values of company by department, product ID and month. Second table contains sales target by department and month. I want to add a column to second table. It should shows sum of values from table 1 with grouping department and month.
For Example:
For 310101 in Februray sum of values is 110. So, the first row of table 2 should be 110.
Can you please help me with this DAX function?
Create a calculated column in both tables which will represent an unique ID (Eg: ID = Table1[Dpt ID] & Table1[Month]) and join the both tables by this ID field to create a relationship between these 2 tables.
Now, select all the columns from Table 2 and also select Value column from Table 1. You should get the sum by department.

Power BI: Grouping results in a many to many relationship

I am new to power BI, I imported 3 tables from SQL server to Power BI Desktop: 2 main tables and 1 for many to many relationship (bridge), and I need to get results from the second main table based on grouping from the first and second main tables.
Tables are: Customers, Orders, OrderCustomers (bridge table). Orders table has a SalesChannelId field, and I need to get each customer's orders grouped by sales channels, and the percentage of all the customer's orders
I already achieved this with a SQL query (which is the thing I am good at):
select
Customers.FirstName,
all_orders.orders_count,
SalesChannels.Name as SalesChannel,
COUNT(OrderCustomers.OrderId) as SaleChannelOrdersCount,
cast (((COUNT(OrderCustomers.OrderId) * 100) / all_orders.orders_count) as nvarchar(3)) + '%' as SaleChannelOrdersPercent
from
Customers
inner join OrderCustomers
on OrderCustomers.CustomerId = Customers.Id
inner join Orders
on Orders.Id = OrderCustomers.OrderId
inner join SalesChannels
on SalesChannels.Id = Orders.SalesChannelId
inner join
(select
Customers.id as customer_id,
COUNT(OrderCustomers.OrderId) as orders_count
from
Customers
inner join OrderCustomers
on OrderCustomers.CustomerId = Customers.Id
where 1=1
group by Customers.id) as all_orders
on all_orders.customer_id = Customers.Id
where 1=1
group by Customers.id, Customers.FirstName, SalesChannels.Name, all_orders.orders_count
order by Customers.id, SalesChannels.Name
With this query I get results like these:
FirstName orders_count SalesChannel SaleChannelOrdersCount SaleChannelOrdersPercent
Adam 9 Online 1 11%
Adam 9 Counter 8 88%
Henrik 3 Counter 3 100%
Mary 15 Online 3 20%
Mary 15 Counter 12 80%
How to achieve the same results using Power BI?