show tow desimals after MS SQL - casting

Hi I have some problem which I can't solve, maybe some of you can help me
I need to show result of division.
select 50/200
as we all know it supposed be 0.25, however, I got 0
so them i try this
SELECT ROUND(CAST(50 AS NUMERIC(18,2) )/ CAST(200 AS NUMERIC(18,2)),2)
which gives me 0.25000000000000000000
I then tried to use Round
select cast(round(50/200,2) as numeric(36,2))
but its returning me 0.00
How would i fix this to just show 0.25?

You could just do this:
SELECT CAST ( ROUND ( 50 / 200.0 , 2 ) AS numeric ( 18 , 2 )) ;
EDIT:
Per your comment, you could modify it to this.
SELECT CAST ( ROUND ( #int1 / CAST ( #int2 AS numeric ( 18 , 2 )) , 2 ) AS numeric ( 18 , 2 )) ;

Related

Applying measure for months -12 to -24 doesnt work

I have 2 measures, where depending on the date filters selected, I show:
Last 12 months:
CALCULATE
(
[Xyz] , DATESINPERIOD ( 'Date'[Date] , MAX('Date'[Date]) , -12 , MONTH )
)
I need to do the same, but for months -12 to -24.
I have tried, but DATEADD doesn’t seem to work with MAX…
var dat = MAX('Date'[Date])
RETURN
CALCULATE
(
[Xyz] ,
DATESINPERIOD(
'Date'[Date],
DATEADD(dat,-12,MONTH) ,-24,MONTH)
)
How can I make the period from -12 to -24 ?
What if you try this ????? Even If I haven't tested it yet. I know you can nest Time Intelligence Functions.
_12_24 Period =
CALCULATE (
[Xyz],
SAMEPERIODLASTYEAR (
DATESINPERIOD ( 'Date'[Date], MAX ( 'Date'[Date] ), -12, MONTH )
)
)

How to check if the value is negative within the given time interval in DAX?

I have a value where I want to check if it is negative or 0 within the first 4 weeks from now.
Product ID Quantity Week Ending
A 5 18/07/2021
A -6 25/07/2021
A 4 29/08/2021
B 2 18/07/2021
B 7 25/07/2021
For example for my sample dataset above, product A is negative within the first 4 weeks from now because it is negative on 25/07/2021. So I want to create a measure that gives me Yes or No based on the condition above for all products. In this case,
Finally, when I use the measure in matrix along with the product ID, It should give me result that resembles below:
Product ID Is Short In 4 Weeks
A Yes
B No
Can anyone please help me with this?
You can create a measure with below code.
Negative_Check =
VAR result =
CALCULATE (
MIN ( 'Table (3)'[Quantity] ),
FILTER (
'Table (3)',
'Table (3)'[Week Ending] >= TODAY ()
&& 'Table (3)'[Week Ending]
<= TODAY () + 28
)
)
RETURN
IF ( result < 0, "Yes", "No" )

ALLEXCEPT not working when filtering blanks

I have a simple problem. My DAX measure does not seem to be working correctly when I filter for non-existing values. Here are some details:
Table:
Column1: A,A,A,A,A,B,B,B,B
Column2: 1,2,3,4,5,1,2,3,5
Measure = calculate(countrows(table), allexcept(column1))
Card Visual returns correct row count when I filter by column1 (any value in filtering pane)
However it returns wrong row count when I filter by column2 = "4" and Column1 = "B" (in filtering pane). It seems that it should ingore filtering by column2 and it does except when I specifically filer for value = "4". It gives "blank" result value in a card visual then.
Any ideas why?
Here's the screen. I would like to populate that blank cell with "4" (in a singe-table data model.enter image description here
In your case you dont need to add allexcept in your measure. Below code would be fine.
TestMeasure = countrows(Test_Data)
PFB screenshot
I am hoping that you have a data model as following
table name _dim1
colA
A
B
C
table name _dim2
colB
1
2
3
4
5
table name _fact
colA
colB
A
1
A
2
A
3
A
4
A
5
B
1
B
2
B
3
B
5
C
2
C
3
If you have this you can reach where you need by using following measures
Measure3 =
CALCULATE ( COUNTROWS ( _fact ), ALL ( _dim2[colB] ), VALUES ( _fact[colA] ) )
Measure9 =
VAR _1 =
MAX ( _dim2[colB] )
VAR _2 =
CALCULATE (
MAXX (
FILTER ( _dim2, _dim2[colB] <= _1 ),
LASTNONBLANKVALUE ( _dim2[colB], [Measure3] )
),
ALL ( _dim2[colB] )
)
RETURN
_2
Measure10 =
VAR _1 =
MAX ( _dim2[colB] )
VAR _2 =
CALCULATE (
MAXX (
FILTER ( _dim2, _dim2[colB] > _1 ),
FIRSTNONBLANKVALUE ( _dim2[colB], [Measure3] )
),
ALL ( _dim2[colB] )
)
RETURN
IF ( ISBLANK ( [Measure9] ) = TRUE (), _2, [Measure9] )
I don't think you can reach here from a single table like following
colA
colB
A
1
A
2
A
3
A
4
A
5
B
1
B
2
B
3
B
5
C
2
C
3

Summarize values based on two columns DAX

I'm new to DAX and don't get how it works (I'm studying its context transitions and so on). What I would like to do is to calculate the marked column without duplicating the stock. I mean, it should only sum stock once by PRODUCT. In this case, instead of 480, it should be 240 in every line (including the one that is 0, not sharing product).
This is my measure:
REF STOCK*LINEA :=
CALCULATE (
SUM ( Production[REF_STOCK] );
FILTER (
ALLEXCEPT (Production; Production[SIAC] );
Production[PENDING_UNITS] > 0
&& Production[SIAC 1] <> "SIAC"
)
)
EDIT: I think I made it.
This measure apparently works, but I'm not sure if it is correct for what I want to achieve. Will it work with every case?
REF STOCK*LINEA :=
CALCULATE (
SUMX (
SUMMARIZE ( Production; Production[SIAC]; Production[REF]; Production[Description] );
[Avg of REF_STOCK]
);
FILTER (
ALLEXCEPT ( Production; Production[SIAC] );
Production[PENDING_UNITS] > 0
&& Production[SIAC 1] <> "SIAC"
)
)
Please confirm it should work as expected. Thank you!

Determining a Style Changeover by Machine using PowerBI

So I have a table that has the output of all machines in a department with styles. For example:
|Machine| |Style| | QTY| |Time| |Date| etc...
1 001 100 8:00AM 5/21/19
2 001 200 8:05AM 5/21/19
1 001 100 9:00AM 5/21/19
1 004 100 10:00AM 5/21/19
2 001 200 9:05AM 5/21/19
I'm looking to see the amount of times a style is changed for a machine. So in this case, for Machine 1 it was one style change and for Machine 2 it was zero.
I've tried adapting some code to no avail; mainly because I'm having trouble understanding the logic and I can't really think of a good index to work with.
Here is what I got so far:
EarliestChange Over Index =
VAR Temp =
CALCULATE (
MAX ( Table[Index] ),
FILTER (
Table,
[Index] < EARLIER ( [Index] )
&& [Style] <> EARLIER ( [Style])
&& Table[Date] = today()-1
)
)
VAR Temp1 =
CALCULATE (
MIN ( [Index] ),
FILTER (
Table,
[Index] > EARLIER ( [Index] )
&& [Style] <> EARLIER ( [Style])
&& Table[Date] = today()-1
)
)
RETURN
IF ( [Index] > temp && OR ( [Index] < temp1, ISBLANK ( temp1 ) ), temp + 1, 0 )
I tried to restrict it to just one day so that I could evaluate the results so that portion can be dropped. I've tried two different indexes, one was the machine number and the other was the difference in time from today and the min date on the table. In a visual, I've been taking a distinct count of the EarliestChange Over Index and subtracted one since it didn't constitute a "change over."
EDIT:
Issue where multiple styles are logged at the same time causing false change overs.
|Machine| |Style| | QTY| |Time| |Date| etc...
1 001 100 8:00AM 5/21/19
1 001 100 9:00AM 5/21/19
1 004 100 10:00AM 5/21/19
1 004 100 10:00AM 5/21/19
1 004 100 10:00AM 5/21/19
In one department a time would never be duplicated. However, in another department (for whatever reason) might log 3 rolls at the same time. This would cause the equation to log 10:00am as 3 change overs. It might be a system glitch why there isn't unique time stamps per roll but this is the case unfortunately.
One way of doing it:
First, I modified your data as follows:
Added a record for Machine 1 at 11:00AM to capture a situation when a style reverts to the old one;
Added a column for Date-Time (simply Date + Time), to make life easier;
Named the table as "Data"
Measure:
Style Change Count
=
SUMX (
Data,
VAR Current_DateTime = Data[Date-Time]
VAR Current_Style = Data[Style]
VAR Previous_DateTime =
CALCULATE (
MAX ( Data[Date-Time] ),
FILTER ( ALLEXCEPT ( Data, Data[Machine] ), Data[Date-Time] < Current_DateTime )
)
VAR Previous_Style =
CALCULATE (
VALUES ( Data[Style] ),
FILTER ( ALLEXCEPT ( Data, Data[Machine] ), Data[Date-Time] = Previous_DateTime )
)
RETURN
IF ( Current_Style = Previous_Style || ISBLANK ( Previous_Style ), 0, 1 )
)
Result:
How it works:
We need to use SUMX to make sure that our subtotals and totals are correct;
SUMX iterates over Data table and for each record computes "Previous date-time", which is simply the max datetime less than the current datatime, per machine (hence ALLEXCEPT);
Then, we can calculate Previous Style, which is a style where date-time = previous date-time;
Finally, we compare current style and previous style. If they are not the same, we add 1;
In addition, I added a test for the starting condition - first occurrence of a machine, for which previous style does not exist yet. I did not treat such records as "style change". If you want to count initial records as style change, remove ISBLANK() part.