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?
Related
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.
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))
Hopefully a quick explanation of what I am hoping to accomplish followed by the approach we've been working on for over a year.
Desired Result
I have a table of SCD values with two columns, SCD_Valid_From and SCD_Valid_To. Is there a way to join a date table in my model (or simply use a slicer without a join) in order to be able to choose a specific date that is in between the two SCD columns and have that row of data returned?
Original Table
ID | SCD_Valid_From | SCR_Valid_To | Cost
1 2020-08-01 2020-08-03 5.00
Slicer date chosen is 2020-08-02. I would like this ID=1 record to be returned.
What We've Attempted So Far
We had a consultant come in and help us get Power BI launched last year. His solution was to create an expansion table that would contain a row for every ID/Date combination.
Expanded Original Table
ID | SCD_Valid_Date | Cost
1 2020-08-01 5.00
1 2020-08-02 5.00
1 2020-08-03 5.00
This was happening originally on the Power BI side, and we would use incremental refresh to control how much of this table was getting pushed each day. Long story short, this was extremely inefficient and made the refresh too slow to be effective - for 5 years' worth of data, we would need over 2000 rows per ID just to be able to select a dimensional record.
Is there a way to use a slicer where Power BI can select the records where that selected date falls between dates in two columns of a table?
Let me explain a workaround and I hope this will help you to solve your issue. Let me guess you have below 2 tables-
"Dates" table with column "Date" from where you are generating the date slicer.
"your_main_table" with with column "scd_valid_from" and "scd_valid_to".
Step-1: If you do not have relation between table "Dates" and "your_main_table", this is fine as other wise you have to create a new table like "Dates2". For this work around, you can not have relation between those tables.
In case you have already relation established between those tables, create a new custom table with this below code-
Dates2 =
SELECTCOLUMNS(
Dates,
"Date", Dates[Date]
)
From here, I will consider "Dates2" as source of your Date slicer. But if you have "Date" table with no relation with table "your_main_table", just consider "Dates" in place of "Dates2" in below measures creation. Now, Create these following 4 measures in your table "your_main_table"
1.
date_from_current_row = max(join_using_date_range[SCD_Valid_From])
2.
date_to_current_row = max(join_using_date_range[SCD_Valid_to])
3.
date_selected_in_slicer = SELECTEDVALUE(Dates2[Date])
4.
show_hide_row =
if(
[date_selected_in_slicer] >= [date_from_current_row]
&& [date_selected_in_slicer] <= [date_to_current_row]
,
1,
0
)
Now you have all instruments ready for play. Create your visual using columns from the table "your_main_table"
Final Step: Now just add a visual level filter with the measure "show_hide_row" and set value will show only when "show_hide_row = 1".
The final output will be something like below image-
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
I want to be able to compare two tables within the same SQLite Database using a C++ interface for matching records. Here are my two tables
Table name : temptrigrams
ID TEMPTRIGRAM
---------- ----------
1 The cat ran
2 Compare two tables
3 Alex went home
4 Mark sat down
5 this database blows
6 data with a
7 table disco ninja
++78
Table Name: spamtrigrams
ID TRIGRAM
---------- ----------
1 Sam's nice ham
2 Tuesday was cold
3 Alex stood up
4 Mark passed out
5 this database is
6 date with a
7 disco stew pot
++10000
The first table has two columns and 85 records and the second table has two columns with 10007 records.
I would like to take the first table and compare the records within the TEMPTRIGRAM column and compare it against the TRIGRAM columun in the second table and return the number of matches across the tables. So if (ID:1 'The Cat Ran' appears in 'spamtrigrams', I would like that counted and returned with the total at the end as an integer.
Could somebody please explain the syntax for the query to perform this action?
Thank you.
This is a join query with an aggregation. My guess is that you want the number of matches per trigram:
select t1.temptrigram, count(t2.trigram)
from table1 t1 left outer join
table2 t2
on t1.temptrigram = t2.trigram
group by t1.temptrigram;
If you just want the number of matches:
select count(t2.trigram)
from table1 t1 join
table2 t2
on t1.temptrigram = t2.trigram;