I wrote one DAX formula in Power BI. The result from this DAX formula is either a number or a blank. If the result is a number, align it to the right, if the result is blank, align it to the middle. How can I do that?
Yeni Teminatlı = IF(
ISBLANK(
CALCULATE(
SUM(Hamdata[Hedef_Tutar]),
FILTER(Hamdata, Hamdata[GroupBuName] = "Teminatli"),
FILTER(Hamdata, Hamdata[Hedef_Tutar] <> 0)
)
), " - ",
CALCULATE(
SUM(Hamdata[Hedef_Tutar]),
FILTER(Hamdata, Hamdata[GroupBuName] = "Teminatli"),
FILTER(Hamdata, Hamdata[Hedef_Tutar] <> 0)
)
)
The result is as follows. Put the "-" sign because the result is not a number. But when this mark is present I want it to be center aligned. How can I do that dynamically?
enter image description here
Choose Matrix --> Format --> Field formatting --> "Yeni Teminatlı" --> Alignment --> Centre
I did this, but if this value is a number, the numbers appear in the center. But when there is Number it needs to appear right aligned.
A very funny trick :) is you add some white spaces to the right and adjust until you find the appropriate position for your value "-"
" - "
You have to turned off the Text Wrap option for as shown below-
Related
I have some Fact Revenue, I am trying to group by Conca, and display the values only if negative…
For doing it I have this calculated column:
=
VAR name1 = Revenue[Conca]
VAR name2=
CALCULATE (
SUM ( Revenue[CostOfQuality] ),
FILTER ( Revenue, Revenue[Conca] = name1 )
)
RETURN
if (name2>0, 0, Revenue[CostOfQuality])
It works:
(highest value is 0 as expected):
Now...
If I drag fiscal year it stops working:
Why is it that I see numbers higher than 0?? (I want it to still work even if I bring other filters...)
a calculated column is computed computed once on the whole dataset after the data are loaded, and is basically a "static column".
Whatever the expected result, you are looking for a measure, which gets computed in the current context
Calculated columns and calculated tables are different from measures, since they are static: You can filter them, but they don't recalculate on filter changes.
I have 2 measures that I want to concatenate while respecting their original format.
The last column should be % ticket type (numberDistinctCount).
For example 59.83% (168,479) but instead when I combine them both, it appears that the formatting was lost and becomes 0.598337950138504 (168479)
The definition I use for the last column is
% count ticket type = [% ticket type] & " (" & [numberDistinctCount] & ")"
Is there a way to ensure that the concatenation of 2 measures respect the individual measure format?
The result of this concatenation will be used as a tooltip
Thanks
You have to wrap your measures in FORMAT functions for this, else Power BI just defaults to the numerical output from your measure - in this case a decimal that is "primed" for percentage formatting.
Try this:
% count ticket type =
FORMAT ( [% ticket type] , "0.00%" ) & " (" & [numberDistinctCount] & ")"
I have created a pareto analysis but the problem is that it's not dynamic because the rankx it's done in a calculated column in customers table in order of the sum of sales in an other table.
Now my #runningtotal is
CALCULATE([M-CY_Sales];FILTER(ALLSELECTED(CUSTOMERS);
CUSTOMERS[DAX RANK]<=MAX(CUSTOMERS[DAX RANK]));CUSTOMERS[Customer Number] <>BLANK();
'Detail Sales Report'[Total Actual Revenue - Base]>0)
where I use the calculated column with rankx CUSTOMERS[DAX RANK]. Can I make this measure dynamic? I was thinking to build a table with var and addcoloumn but I'm not able to do it. My actual problem is that I need this pareto dynamic because the filter for district does not function with static column.
I was trying to write something but I don't know how I could create what I want
#RUNNINGTOTAL2 =
var customerranked=ADDCOLUMNS(ALLSELECTED(CUSTOMERS);"ranking";[M-DAX RANK])
return
CALCULATE([M-CY_Sales];FILTER(ALLSELECTED(CUSTOMERS);
customerranked<=MAX(customerranked));CUSTOMERS[Customer Number]<>BLANK();
'Detail Sales Report'[Total Actual Revenue - Base]>0)
Obviously this is not correct. I hope you understand my problem. I need to refer a virtual column done with rankx in my measure running total
Sample data edited with measures: [here]: https://mega.nz/#!4t1y0AJI!XF2Vcejm6C50nnssQCS1bJEhnqIGiH1d-mIltVskRgE
While here is the PBIX file and it may work as you expected, but you should take a broom and sweep your model a little. To get it working just set up the relationship from District to Customer and then to Sales. Or even better, get rid of Districts table. You have that dimension in Customers table. I just slightly changed your measures to get it working but I would change them altogether. Probably you do not need to use FILTER function.
#RUNNINGTOTAL =
CALCULATE (
SUM ( 'Sales'[Revenue] ),
FILTER (
ALLSELECTED ( Customers ),
Customers[DAX RANK]
<= MAX ( Customers[DAX RANK] )
),
'Sales'[Revenue] > 0
)
Anyway I would start it from scratch.
Why do you have three tables? What is the purpose of table Districts. You can use the Districts form table Customers to slice Sales.
If you really do not accept corrected invoices and negative sales (ask yourself why), build a measure like that:
[Sales] =
CALCULATE (
SUM ( FactTable[Sales] ),
FactTable[Sales] > 0
)
And then refer to it in other measures. Check these posts to see differences of filtering:
DAX Calculate function with and without FILTER
Difference between CALCULATE(m, x=red) vs CALCULATE(m, KEEPFILTERS(x=red))
You may think of building a bridge table, between Customers and Sales, which will contain unique CustomerID of both tables. Dictionaries are updated with lag.
bridge =
DISTINCT (
UNION (
DISTINCT ( Sales[CustomerID] ),
DISTINCT ( Customers[CustomerID] )
)
)
Give it a shot: https://www.daxformatter.com/
It is indeed possible, and encouraged to define measures that calculates ranks and cumulative totals on the fly.
However, there are some visualization issues. It looks not possible to use a measure for x axis with "Line and clustered column chart". So it would not be possible to use the Rank measure for x axis. You may put Customer Number to x axis instead, however the chart will look badly with a categorical x axis. It will not fit in the screen and will require a long scroll to reach the right end. Practically, this will hardly work as a pareto chart.
On the basis of this observation, I suggest to use R / Python visual if possible. Here is an example with R visual.
library(dplyr)
library(ggplot2)
totalSales <- sum(dataset$SalesAmount)
dataset <- dataset %>%
arrange(desc(SalesAmount)) %>%
mutate(
CumulativeSales = cumsum(SalesAmount),
Rank = order(SalesAmount, decreasing = TRUE)
)
p <- ggplot(dataset, aes(x = Rank, y = SalesAmount)) +
geom_bar(stat = "identity", width = 1, fill = "#01b8aa")
ymax <- layer_scales(p)$y$range$range[2]
p <- p + geom_line(aes(y = CumulativeSales / totalSales * ymax),
size = 1, color = "#fd625e") +
scale_y_continuous(sec.axis = sec_axis(~ . * totalSales / ymax)) +
theme_bw()
p
I have a matrix that I am using in Power BI visualization.
Percentage Status
High Low Medium
10% 1
20% 1
30% 1
"1" is the count of values in a column. I want to display 0 where there is no data in the above matrix.
I have tried dong below:
Adding +0 at the end of measure.
= IF(CALCULATE(COUNT(Table[col])=ISBLANK()),0,COUNT(Table[col]))
But, nothing seems to work as it is not considering no data as blank.
I don't think you can do this with just a measure. There are no rows of data in the table that correspond to those empty cells, so it doesn't even try to evaluate a measure.
What you can do is create a calculated table and use that in your Columns field.
Status = VALUES(Table[Status])
Create the relationship from that table to your original table on the Status column and replace Table[Status] with Status[Status] in your matrix visual. Now adding +0 should do the trick.
Measure = COUNT ( Table[col] ) + 0
You are checking the True False condition with ISBLANK(), use directly value as Blank() to compare the cell value,
=
IF (
CALCULATE (
COUNT ( Table[col] )
= BLANK ()
),
0,
COUNT ( Table[col] )
)
Hope it helps!!
Please check it here..
I have started to learn some basic usage in Power BI and need help in solving a problem.
Please see my table attached.
I want to calculate the average of the percentage of the last entry with the last "Status 1" for each ID. In my database there are some ID's in which the Status "Status 1" is just contained once (like ID 4) - I would like to exclude those ID's. Is that possible?
For this example it would be:
(94 + 82 + 85) / 3 = 87
I have started with trying to calculate the average of all "Percent" for all "Status 1".
Test Average 2 =
CALCULATE (
AVERAGEX(Tabelle1; Tabelle1[Percent]);
FILTER ( ALL ( Tabelle1 ); Tabelle1[Status]="Status 1" );
ALL (Tabelle1)
)
How do I proceed? I don't know how to create the code with so many conditions. I have tried to research the solution but gave up. I hope you can help me and would appreciate the help for a Coding beginner :)
Thanks in advance,
Jenny
First, welcome to the Power BI family!
Second, here is how I would approach your problem...
1. Create a "DateTime" column
Since we need to check for the latest entry, and presumably you want that checked based on date and time, it is easier if there is only one column to check).
In the ribbon, go to Modeling -> New Column.
In the formula bar that pops up, enter the following formula. Power BI can recognize the date and time columns for what they are and since they are both elements of date/time, Power BI lets us do simple addition. The - 1 in the formula is to account for the "day" that is associated with the time.
DateTime = [Date] + [Time] - 1
2. Create Average Measure
Here is the whole formula first and following is a breakdown piece by piece.
Last Percent Average =
VAR TargetStatus = "Status 1"
VAR IDsWithMoreThanOneStatus1 = FILTER('Data', COUNTROWS(FILTER('Data', IFERROR(SEARCH(TargetStatus, [Status]), -1) > 0 && [ID] = EARLIER([ID]))) > 1)
VAR LastStatus = FILTER(IDsWithMoreThanOneStatus1, IFERROR(SEARCH(TargetStatus, [Status]), -1) > 0 && [DateTime] >= MAXX(FILTER('Data', IFERROR(SEARCH(TargetStatus, [Status]), -1) > 0 && [ID] = EARLIER([ID])), [DateTime]))
RETURN
DIVIDE(SUMX(LastStatus, [Percent]), COUNTROWS(LastStatus), BLANK())
The first chunk of the measure is defining variables. Variables are super useful and I would encourage you to at least take a quick look at them. Here's just one article on them.
The first variable is simply designating the target status (in case you ever want to change it). This variable is optional as if it will never change, you could just put the text into the other formulas.
VAR TargetStatus = "Status 1"
The second variable filters the master list of data to only those IDs with more than one "Status 1" (excluding ID = 4 in this case). One of the keys with this variable is the EARLIER function. While it is horribly named (in my opinion), it is immensely useful. You can read the official documentation on it here.
Step by step through the variable: We want to filter the Data table... where the row count is greater than one... when we filter the table to include "Status 1" for the specified ID.
The SEARCH function will look at the entire string and tell us the starting position of our search string (TargetStatus). We need to wrap it in an IFERROR function to handle cases where TargetStatus does not show up in the [Status] value. Then we just check to see if the result is greater than 0 indicating that the TargetStatus is in the [Status] value.
VAR IDsWithMoreThanOneStatus1 =
FILTER(
'Data',
COUNTROWS(
FILTER(
'Data',
IFERROR(SEARCH(TargetStatus, [Status]), -1) > 0 &&
[ID] = EARLIER([ID])
)
) > 1
)
The third variable filters the list from the second variable to capture the last "Status 1" for each ID. This uses the same thought process as the second variable: we want to filter the table (from the second variable) where the status = "Status 1" and the [DateTime] is greater than or equal to the max [DateTime] for all "Status 1" for the specified ID.
VAR LastStatus =
FILTER(
IDsWithMoreThanOneStatus1,
IFERROR(SEARCH(TargetStatus, [Status]), -1) > 0 &&
[DateTime] >= MAXX(
FILTER(
'Data',
IFERROR(SEARCH(TargetStatus, [Status]), -1) > 0 &&
[ID] = EARLIER([ID])
),
[DateTime]
)
)
The RETURN keyword is just telling Power BI that we are done defining variables and that everything else in the formula defines the actual value that is returned for this measure.
RETURN
Lastly is the average itself. In this case, we'll use SUMX to sum up all of the [Percent]s from the third variable, and then divide it by the row count of the third variable. I would always recommend using the DIVIDE function as it handles for dividing by 0 and other errors.
DIVIDE(
SUMX(
LastStatus,
[Percent]
),
COUNTROWS(LastStatus),
BLANK()
)
When it's all said and done, your measure will give the desired result.