How to conditionally display decimals in a measure in Power BI - powerbi

Let's say I have a simple measure like this:
SimpleMeasure = MyTable[Column1]/MyTable[Column2]
Is it possible to have the number of displayed decimals dependent on the value? For example, if my measure calculates the number 500, then I don't want to see 500.00 but rather 500. However if my measure calculates 0.56, then I want to see the value displayed like that to two decimal places and not rounded to the number 1.
So a possible visualization table would look like this:
Store SimpleMeasure
00 10
01 18
02 0.67
03 6
Thank you in advance!

IF(
[SimpleMeasure] < 1, FORMAT([SimpleMeasure], "#,##0.##",
FORMAT([SimpleMeasure], "#,##0")
)

You can use the Switch() function. It will return the first True result. Put you values in a DESC order - start with the largest value, then come to a lower and then to the lowerest. For example :
Switch(
True
,[SimpleMeasure]<1,Round([SimpleMeasure],2) -- 0.655->0.66
,[SimpleMeasure]>999,Round([SimpleMeasure],-2) --1044->1000
,[SimpleMeasure]>499,Round([SimpleMeasure],-1) -- 505,5->510
,[SimpleMeasure]>99,Round([SimpleMeasure],0) -- 101,55->102
,[SimpleMeasure]>1,Round([SimpleMeasure],1) -- 99,43->99.4
)
Or/and you can use in the Switch() the Format() function, with or instead of Round() like:
Format([SimpleMeasure],"#,##0.#")
Or
Format(Round([SimpleMeasure],1),"#,##0.#")
-- Both of them Format() will return and show 99,43 as 99,4.
-- Placeholer # returns a digit or nothing.
-- Placegholder 0 returns a digit or 0.
Usage of Format() and Round() depends on your task. But for a selective Fomat you will need the Switch().
Hope, the answer will help you solve the case

Related

DAX to Test for Whole Number

I have a Actuals column like so:
ID | Airport
------------
A | 98.4
B | 98.0
C | 95.3
I'm attempting to format the numbers above into percentages for a front-end report. I have this written in a switch statement - for ease I'll just write the logic as an IF boolean.
example_measure =
VAR Nums = SELECTEDVALUES(Table[Actuals])
VAR FormatNums = IF(DIVIDE(ROUND(nums,1), nums) = 1,
format(nums,"0%"),format(nums,"0.0%")
-
RETURN
FormatNums
no matter what I do this always returns a number with a floating point value of 1f
so in my raw data of 98.0 I'm expecting the format to return "98%" rather than "98.0%"
the measures are used on individual cards, and are filtered so they can only ever show one value or blank, meaning they will only ever display one value on their own.
I've toyed with using if(nums = int(nums) which should evaluate to true for 98.0 but it I always get false.
There is a simpler way - just use built-in formatting codes:
Formatted Actuals =
VAR x = SELECTEDVALUE(Data[Actuals])
RETURN
FORMAT(x, "General Number") & "%"
Result:
Built-in style "General Number" handles your situation correctly, you just need to add % symbol to the formatted string. No need to test for whole numbers.
To convert a column/measure into percentage, you can simply divide the column by 100 and then format it as a percentage. Use the following steps:
Create a new column/measure: Perc_value = Table[Actuals]/100
Then go into the modelling tab, select the created column/measure and format it as a % and limit the number of decimal places to 0
This should give the view you are looking for. Hope this helps.
Edit:
You can use the below formula to achieve the desired result:
Column4 = IF('Table'[Column2]-ROUND('Table'[Column2],0)=0,FORMAT('Table'[Column2]/100,"0%"),FORMAT('Table'[Column2]/100,"0.0%"))
Replace Column2 withyour field and you should be good to go.

Using MINIF/MAXIF with strings containing numbers?

I want to find the minimum number with given conditions(is writer and is under probation), the below code works if D contains numbers, but how do I do it if the number is a part of a string, like a fraction for example? Like how do I use this formula if numbers in D look like "1/8", "31/688", "21/33", etc?
=MINIFS(D3:D1007, A3:A1007, "Writer", C3:C1007, "Probation")
I already have another formula that I use that calculates a decimal value given the fraction, If the fraction is in cell D21 then it would look like this:
=left(D21,find("/",D21)-1)/(right(D21,len(D21)-find("/",D21)))
but how do I apply this kind of formula in a minif/maxif?
I have attached a picture to show what I mean, what I'm trying to do is to put a formula in the passed/total column of package stats(probation), and it will get the lowest passed/total value out of the ones with that package name and importance level. as you can see, the entire writer package's pass rate is 5/8 because the lowest pass rate out of the writer package 5/8 is the lowest pass rate out people with package=writers and importance = probation. But at the moment I have to enter the 5/8s manually, I want it to be able to get it automatically using the formula I'm trying to figure out above.
try:
=ARRAYFORMULA(MIN(IF((A3:A="writer")*(C3:C="probation"),
IFERROR(REGEXEXTRACT(D3:D, "\d+")/REGEXEXTRACT(D3:D, "/(\d+)"), D3:D), )))
or to return fraction:
=ARRAYFORMULA(VLOOKUP(MIN(IF((A3:A="writer")*(C3:C="probation"),
IFERROR(REGEXEXTRACT(D3:D, "\d+")/REGEXEXTRACT(D3:D, "/(\d+)"), D3:D), )),
{IF((A3:A="writer")*(C3:C="probation"),
IFERROR(REGEXEXTRACT(D3:D, "\d+")/REGEXEXTRACT(D3:D, "/(\d+)"), D3:D), ), D3:D}, 2, 0))
also make sure fractions are formatted as plain text not date

"ROUND" calculation can't be lower than 1.0

I need a ROUND calculation to always round up when it lands between 0 and 1 (but not when it's a value above these numbers), but can't seem to figure out how to make it work.
This is what I have currently:
=ROUND(100/DATA!H6)
try:
=IF((100/DATA!H6>0)*(100/DATA!H6<1), ROUNDUP(100/DATA!H6, ), 100/DATA!H6)

IF statement with concatenate and round in Excel

I am trying to create an IF statement formula in excel that converts minutes to days and hours depending on the amount. Then rounds the value to 1 or 2 decimal points and adds the descriptive text (days, hours, etc) to the end
I have tried the following which converts and adds text but does not round:
=IF(L15>=1440, CONVERT(L15,"min","day") & CONCATENATE(L15," days"),
IF(L15>=60, CONVERT(L15,"min","hr") & CONCATENATE(L15," hours"),
IF(L15<=59, CONVERT(L15,"min","min") & CONCATENATE(L15," mins"))))
I would adjust your formula as follows and based on you wanting to round the final number
=IF(L15>=1440,ROUND(L15/1440,2)&" days",IF(L15>=60,ROUND(L15/60,2)&" hours",ROUND(L15,2)&" minutes"))
the ,2 in the ROUND function tell excel how many decimal places to calculate to. if format is set to general, trailing 0s will not be displayed. If you only want 1 decimal calculation then change the ,2 to ,1.

Make =IF Function Output Numbers For "Scoring": Google Sheets

I'm am exploring methods of giving scores to different datapoints within a dataset. These points come from a mix of numbers and text string attributes looking for certain characteristics, e.g. if Col. A contains more than X number of "|", then give it a 1. If not, it gets a 0 for that category. I also have some that give the point when the value is >X.
I have been trying to do this with =IF, for example, =IF([sheet] = [Text], "1","0").
I can get it to give me 1 or 0, but I am unable to get a point total with sum.
I have tried changing the formatting of the text to both "number", "plain text", and have left it as automatic, but I can't get it to sum. Thoughts? Is there maybe a better way to do this?
FWIW - I'm trying to score based on about 12 factors.
Best,
Alex
The issue here might be that you're having the cell evaluate to either the string "0" or the string "1" rather than the number 0 or the number 1. That would explain why you're seeing the right things but the math isn't coming out right - the cell contents look like numbers, but they're really text, which the summation would then ignore.
One option would be to drop the quotation marks and write something like this:
=IF(condition, 1, 0)
This has the condition evaluate to 1 if it's true and 0 if it's false.
Alternatively, you could write something like this:
=(condition) * 1
This will take the boolean TRUE or FALSE returned by condition and convert it to either the numeric value 1 (true) or the numeric value 0 (false).