I have a survey data in a DWH and I want to calculate satisfaction rate,
I was able to do so with SQL server query, but due to lack of knowledge in DAX I couldn't achieve it as a measure in power BI using DAX.
The data warehouse contains 3 tables plus the DIM_DAT, bellow is a sample of the data:
Data model:
The formula to calculate the satisfaction:
(Count of RESPONDENT where ANSWER_VALUE >= 4 for each question (group by QUESTION_DESC).
divided by the number of answers for each question)
grouped by QUESTION_PRIMARY_STAGE and multiply by the weight (the weight is different for each QUESTION_PRIMARY_STAGE)
I was able to achieve the desired result by SQL server query, but because it's only the final result (satisfaction I can't do further analytics on it for example : satisfaction by department / Question / Stage).
.
My SQL server query:
SELECT SURVEY_NAME, SUM(FINALPERC) as SATISFACTION -- sum the satisfaction of the Stages
FROM
(
SELECT SURVEY_NAME, QUESTION_PRIMARY_STAGE,
(AVG("4-5 %") * WEIGHT)/100 AS FINALPERC -- multiply by the weight of each QUESTION_PRIMARY_STAGE and divid it by 100 (the sum of the weight for the stages are 100)
FROM (
select tmp0.SURVEY_NAME, tmp0.QUESTION_DESC, tmp0.QUESTION_PRIMARY_STAGE, tmp0.WEIGHT ,
("4-5"*1.0 / tmp1.COUNTR) as "4-5 %" -- divide by the number of answers for each question
from(
select da.SURVEY_NAME, QUESTION_DESC, da.QUESTION_PRIMARY_STAGE, da.WEIGHT ,
count(fr.RESPONDENT_SRGT) as "4-5" -- Count of RESPONDENT where ANSWER_VALUE >= 4 for each question (group by QUESTION_DESC)
from [dbo].[DWH_QLT_FACT_SURVEY_RESPONSES] fr
join [dbo].[DWH_QLT_DIM_QUESTION_ANSWER] da
on fr.QUESTION_ANSWER_SRGT = da.QUESTION_ANSWER_SRGT
and da.ANSWER_VALUE >= 4
group by da.SURVEY_NAME, QUESTION_DESC, QUESTION_PRIMARY_STAGE, WEIGHT
) tmp0
join
(
--------------------------------Get the number of answers for each question----------------------
select da.SURVEY_NAME, QUESTION_DESC, COUNT(RESPONDENT_SRGT) COUNTR
from [dbo].[DWH_QLT_FACT_SURVEY_RESPONSES] fr
join [dbo].[DWH_QLT_DIM_QUESTION_ANSWER] da
on fr.QUESTION_ANSWER_SRGT = da.QUESTION_ANSWER_SRGT
group by da.SURVEY_NAME, QUESTION_DESC
)tmp1
on tmp0.QUESTION_DESC = tmp1.QUESTION_DESC
-------------------------------------------------------------------------------------------------
)TMP2
GROUP BY SURVEY_NAME, QUESTION_PRIMARY_STAGE,WEIGHT
)TMP2
GROUP BY SURVEY_NAME
Problem:
I want to convert this formula in DAX.
Original masked data are in the shared excel sheet bellow:
https://1drv.ms/x/s!Aq0hOy_0CV0IhD04ClHKc9F-DP2H?e=s5dhjG
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.
Power BI newbie here and I'm trying to figure how to craft my DAX to manipulate my measure values based on certain criteria in the other two tables.
Currently I have 2 separate tables which are joined by a One to Many relationship and a separate Measures table. (Total Sales Price is computed as sum of Sales Price)
My aim is to create a new measure where Total Sales Price is multiplied by 1.5x when DIM_Product_Type[Product Category] = "High".
New Measure =
CALCULATE (
SUM ( FACT_PriceDetails[Sales Price] ),
FILTER ( DIM_Product_Type, DIM_Product_Type[Product Category] = "High" )
) * 1.5
However this returns no values in my visual and I'm trying to discern if its a matter of the table joins or the DAX expressions.
Thank you for your time!
Your measure seems good.
It will select only those products with a Product Category of "High" and multiply them by 1.5 to give you result. i.e. Give me the sum of all "High" Product category Price details multiplied by 1.5.
What you need to check is:
Product Serial Numbers match across the two tables
Your Product Category does indeed contain the category "High"
You have entries in FACT_PriceDetails that link to a DIM_Product_Type that has a category of "High"
Check you have not set any filters that could be hijacking your results (e.g. excluding the "High" product category product type or the realated fact/s)
Option-1
You can do some Transformation in Power Query Editor to create a new column new sales price with applying conditions as stated below-
First, Merge you Dim and Fact table and bring the Product Category value to your Fact table as below-
You have Product Category value in each row after expanding the Table after merge. Now create a custom column as shown below-
Finally, you can go to your report and create your Total Sales measure using the new column new sales price
Option-2
You can also archive the same using DAX as stated below-
First, create a Custom Column as below-
sales amount new =
if(
RELATED(dim_product_type[product category]) = "High",
fact_pricedetails[sales price] * 1.5,
fact_pricedetails[sales price]
)
Now create your Total Sales Amount measure as below-
total_sales_amount = SUM(fact_pricedetails[sales amount new])
For both above case, you will get the same output.
Using PowerBI linked to two separate Access Databases.
I have two datasets containing cost estimates. The cost estimates in Dataset 1 run through 2054; the cost estimates in Dataset 2 run through 2074. I used the Append function to join the two tables together and used the Quick Measure for Running Total to create values for cumulative cost by year. I charted this measure and noticed a significant decrease between 2054 and 2055 and was able to determine that the decrease is the cumulative value for Dataset 1. Does anybody know any ways to fix this?
Roughly explained:
Dataset 1 through 2054 totals to 4.5M.
Dataset 2 through 2054 totals to 3M
Dataset 2 through 2055 totals to 3.25M
Appended Dataset through 2054 totals to 7.5M
Appended Dataset through 2055 totals 3.25M instead of the expected 7.75M
I think the issue might be caused by Dataset 1 not having a value for 2055 or after, but I'm not sure how to resolve this issue.
The measure I'm using is:
Cumulative Cost =
CALCULATE(
SUM('AppendedQuery'[Value]),
FILTER(
ALLSELECTED('AppendedQuery'[Year]),
ISONORAFTER('AppendedQuery'[Year], MAX('AppendedQuery'[Year]), DESC)
)
)
ETA: Picture to explain
Here is your Dataset 1-
Here is your Dataset 2-
Here is your final Dataset after appending Dataset 1 & 2
And finally, here is the output when you are adding column Year and Cumulative Cost to a table visual. As standard PBI behavior, this is just grouping data using column Year and and applying SUM to the column Cumulative Cost.
The calculations are simple-
2051 > 1 + 1 = 2
2052 > 2 + 2 = 4
2053 > 3 + 3 = 6
2054 > 4 + 4 = 8
2055 > 5 = 5
2056 > 6 = 6
=========================
Solution for your case:
I already said in the comments that the solution current data will be not a standard one and will consider fixed $1 per year per department. But if you are happy with this static consideration, you can apply these following steps to achieve your required output-
Step-1 Create a Custom Column as below (Adjust the table name as per yours)-
this_year_spent = IF('Dataset 3'[Cumulative Cost] = BLANK(),0,1)
Step-2 Create the following Measure-
cumulative =
VAR current_year = MIN('Dataset 3'[Year])
RETURN
CALCULATE(
SUM('Dataset 3'[this_year_spent]),
FILTER(
ALL('Dataset 3'),
'Dataset 3'[Year] <= current_year
)
)
Here is the final output-
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?
I am a complete newbie to Power BI
I am trying to reproduce this type of calculation in the Power Query Editor of Power BI.
The screenshot is from Excel and in column C it uses AVERAGE($B$2: B2) and the next row is AVERAGE($B$2: B3) and so on.
C2 = ((168 + 313) / 2) = 241
C3 = (((168 + 313) + 301) / 3) = 261
and so on
Is there a way to do this type of calculation in Power Query?
In the Power Query M language, you can do a similar thing. Filter the rows up to and including the current row's date and then average the CallsPresent column.
= List.Average(
Table.SelectRows(
#"[Previous Step Name Here]",
(C) => C[FullDate] <= [FullDate]
)[CallsPresent]
)
The #"[Previous Step Name Here]" bit is simply the table that you are doing the selection on. A query in the query editor is generally a list of steps where you do one transformation at a time. So your new step is creating a custom column based on the previous step.
The (C) => syntax is a bit more tricky, but basically, it's used to allow me to compare the FullDate in the table we're operating on (#"[Previous Step Name Here]") with the FullDate in the current step. Check out this blog post for much more info related to this.
You can use an average function on the dates up to the current one.
As a calculated column, it would look something like this:
AvgPresented =
CALCULATE(
AVERAGE(
Table1[CallsPresent]),
ALL(Table1),
Table1[FullDate] <= EARLIER(Table1[FullDate]
)
)
or this:
AvgPresented:
AVERAGEX(
FILTER(
Table1,
Table1[FullDate] <= EARLIER(Table1[FullDate])
),
Table1[CallsPresent]
)
Note: The EARLIER function is referring to the earlier row context (i.e. the values in the current row) and has nothing to do with times or dates.