I would like to create a DAX formula with a IF statement.
my logic would be :
IF(column[1]= "sales" && column[2] ="chicago"; SUM('Table'[SalesAmount]);
IF(column[1]= "sales" && column[2] ="sanfranciso"; SUM('Table'[SalesAmount]);
IF(column[1]= "sales" && column[2] ="newyork"; SUM('Table'[SalesAmount]);
IF(column[1]= "sales" && column[2] ="hoston"; SUM('Table'[SalesAmount]);
So, i need to calculate sales by city. how can we write above logic in dax expression in power bi?
You can use the CALCULATE function with your conditions.
For example, let's use it to calculate the sales amount of chicago
chicago_sales_amount = CALCULATE(SUM('Table'[SalesAmount]);column[1]= "sales" && (column[2] = "chicago" || column[2] = "sanfranciso" || column[2] = "newyork" || column[2] = "hoston"))
This above expression will calculate the sum of sales, only for the rows that respect the given condition.
Now you can apply the same logic for the other condition's.
CALCULATE documentation
If you want to get the sum by city but only want it when column[1] = "sales" you can summarize based on a filter:
SumByCity =
VAR curCity = 'Table'[column[2]]
RETURN
CALCULATE(SUM('Table'[SalesAmount]), FILTER(curCity = 'Table'[column[2]] && 'Table'[column[1]]= "sales"))
Related
I have Data like this and I want to create a matrix visual. Since I need the Amount and Percentage in one column inside the matrix, I have created the following structured table in Power BI:
Description
Value
Sales Amount
50000
Sales Percentage
12%
Sales Amount
25000
Sales Percentage
25%
Sales Amount
75000
Sales Percentage
64%
Since it's not possible to store different format types in a single column in Power BI the percentage is stored as decimals and I created a measure to change the format based on the description column with the following code:
Value_formated =
VAR Val = SUM ( Table[Value] )
RETURN
SWITCH (
SELECTEDVALUE ( 'Table'[Description] ),
"Sales Amount", FORMAT ( Val, "0" ),
"Sales Percentage", FORMAT ( Val, "0.00%" ))
My question is how am I able to create a conditional formating to change the underlying color based on the percentage Value? Like for example if the percentage is negative, the percentage field should be red and if positive green. But since percentage is mixed with total number, how can I only filter the percentages? I have tried the following guide: https://xyloslearning.com/en/power-bi-using-a-measure-to-set-up-conditional-formatting/ but I couldn't select the coloring measure maybe because there are two different formats? Can anybody help me?
Thanks!
Create a format measure as follows:
Format =
VAR v = MIN ( 'Table'[Value] )
VAR d = MIN ( 'Table'[Description] )
RETURN
SWITCH(TRUE(),
d = "Sales Percentage" && v < 0, 0,
d = "Sales Percentage" && v >= 0, 1,
2)
Apply conditional formatting as follows:
I have SKUs data with some financial information. I have grouped the SKUs in 4 categories using a measure with SWITCH. Now I would like to summarise the information (e.g. total revenues of the products in each category) but I cannot get the summary to add up to the correct value.
Data model:
FactTable: fact table containing financial information (product code, revenue, profit, period, etc.)
DimSKU: mapping table with SKUs data (product code, brand, segment, etc.)
DimDates: date table (period, months, etc.)
Creating 4 product categories:
(using a measure and not a calculated column as the users can use a dropdown list and select different periods)
My Quadrants =
VAR
Quadrants =
SWITCH(
TRUE(),
AND([SKU_profit] >= [Total_profit], [SKU_growth] >= [Total_growth]), "Maintain",
AND([SKU_profit] < [Total_profit], [SKU_growth] < [Total_growth]), "Address",
AND([SKU_profit] >= [Total_profit], [SKU_growth] < [Total_growth]), "Grow",
AND([SKU_profit] < [Total_profit], [SKU_growth] >= [Total_growth]), "Investigate"
)
RETURN
Quadrants
Calculating YTD sales:
YTD revenue =
VAR
YTD_revenue = TOTALYTD(SUM(FactTable[Revenue]), DimDates[Date])
RETURN
YTD_revenue
Summarising data:
I can add the measure to a visual like a table and it works perfectly fine as long as I also display all the SKUs. Ideally, I would like a summary like the following:
MyQuadrants / Revenue
Maintain / 1,000
Address / 250
Grow / 500
Investigate / 350
I would be grateful if anyone could point me in the right direction.
Thanks all.
Edit
I have managed to make it work thanks to David's suggestion. See below for reference.
Creating configuration table
ConfigTable =
VAR
Total_profit = [Total_profit]
VAR
Total_growth = [Total_growth]
RETURN
{
("Maintain", Total_profit, 1, Total_growth, 1),
("Address", 0, Total_profit, 0, Total_growth),
("Grow", Total_profit, 1, 0, Total_growth),
("Investigate", 0, Total_profit, Total_growth, 1)
}
// 5 columns: quadrant, min profit, max profit, min growth, max growth
// Use table constructor to use variables
Creating measure
YTD revenue (segmented) =
CALCULATE(
[YTD revenue],
FILTER(
VALUES(DimSKU),
NOT ISEMPTY(
FILTER(
ConfigTable,
(
[SKU_profit] >= ConfigTable[min profit] &&
[SKU_profit] < ConfigTable[max profit] &&
[SKU_growth] >= ConfigTable[min growth] &&
[SKU_growth] < ConfigTable[min growth]
)
)
)
)
I have a table which looks like this:
I need to calculate sum of all numbers in column :ADD , IF the number in column CHANGE is 0 AND column DELETE is 0.
The actual data looks like below. Its same as above. The items with red backets should be filtered out because there are values against delete and change for each unique number in MEMO column.
Create a Measure as below to Exclude records from the list-
exclude =
VAR current_row_tsframe_account = MIN(your_table_name[TSframe_Account])
VAR current_row_tsframe_memo = MIN(your_table_name[TSframe_Memo])
VAR find_delete_change_for_memo =
CALCULATE(
COUNT(your_table_name[TSframe_Memo]),
FILTER(
ALL(your_table_name),
your_table_name[TSframe_Memo] = current_row_tsframe_memo
&& your_table_name[TSframe_Account] = current_row_tsframe_account
&& your_table_name[TSframe_Mode] IN {"CHANGE","DELETE"}
)
)
RETURN
IF(
find_delete_change_for_memo > 0,
"Yes",
"No"
)
The above Measure will return Yes/No per row. You can now Apply visual/page level filter so that records only show where measure Exclude = No. Now this below measure will show your expected value-
total = SUM(your_table_name[TSframe_Memo])
I have 3 columns in a table: Date, Product, and Volume. Based on this is I want to calculate the cumulative sum (or YTD) column for each of the products.
I know that to calculate cumulative total we use:
YTD_Actual = CALCULATE(Sum(Combined[Actual Volume]),FILTER(Combined,Combined[Date] <= EARLIER(Combined[Date])))
But I want this to additionally filter individual products and do this calculation for that product.
The expected column is shown in the picture below:
As far as you manage to get the Month column in date format, the following works:
YTD =
VAR currProduct = Table1[Product]
VAR currMonth = Table1[Month]
VAR ytdTotal =
CALCULATE(
SUM(Table1[Volume]),
FILTER(
ALL(Table1),
Table1[Month] <= currMonth &&
Table1[Product] = currProduct
)
)
RETURN IF(NOT(ISBLANK(ytdTotal)), ytdTotal,0)
Result:
I have data in a table and depending on their sale I have to assign a cost:
For example, if have a sale of 200,000 my cost will be 61,298.
I have tried to make a measure to get this but with my current formula, I only get the Sum of all of the values of Cost.
Cost of Sale = CALCULATE(SUM('Cat_VEA0 Cost'[Cost]),
FILTER('Cat_VEA0 Cost',
[Sales] >= MIN('Cat_VEA0 Cost'[From]) &&
[Sales] < MAX('Cat_VEA0 Cost'[To])))
So I don't know, how to get the value of Cost, depending on which range of Sales it is each shop.
You can use MAXX() supposing you want to match only one row in 'Cat_VEA0 Cost' and you want the row with the maximum price.
Solution
Cost of sale = MAXX(
FILTER(
'Cat_VEA0 Cost',
AND(
'Cat_VEA0 Cost'[From] <= SUM(Sheet2[Sales]),
SUM(Sheet2[Sales]) < 'Cat_VEA0 Cost'[To]
)
),
[Cost]
)
Explanation
Get the maximum [Cost] from all the rows that meet the condition:
MAXX(..., [Cost])
Set the condition to [From] <= SUM([Sales]) <= [To]:
FILTER(
'Cat_VEA0 Cost',
AND(
'Cat_VEA0 Cost'[From] <= SUM(Sheet2[Sales]),
SUM(Sheet2[Sales]) < 'Cat_VEA0 Cost'[To]
)
)