Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Magnusnilsson
Frequent Visitor

Build a SPC warning in DAX

Hi,

 

I cant find that this topic has been brought up before.

I am trying to find a way to make a DAX command that gives me a mark when a couple of factors are happening.

 

I am following a process in a industri.

The process is giving me a value that I compare to a target and show this as a % difference by date it is recorded.

Each value is connected to a customer.

 

Can I get code in DAX that give me a let say a "1" when value having 5 times in a row over target...? by customer.

 

Table

Magnusnilsson_0-1703230751130.png

 

Thanks in advance

 

 

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @Magnusnilsson 

I'm sorry for the mistake in my previous answer, I redid it according to your needs:

I created a table using the data you provided:

vzhengdxumsft_0-1704347849450.pngvzhengdxumsft_1-1704347849452.png

Then a calculated column is created:

warn =
var Greater_ = COUNTROWS(FILTER('Table',[Date]<=EARLIER('Table'[Date])&&[Customer]=EARLIER('Table'[Customer])&&[%]>15))
var Lower_ =COUNTROWS(FILTER('Table',[Date]<=EARLIER('Table'[Date])&&[Customer]=EARLIER('Table'[Customer])&&[%]<-15))
RETURN IF([%]>15&&Greater_>=5,1,IF([%]<-15&&Lower_>=5,1))

Greater_ and Lower_ are counted for the same user who has exceeded 15 consecutive years and is below 15 before this date, respectively
The result is as follow:

vzhengdxumsft_2-1704347937341.png

The reason for the discrepancy between the final result and your result may be that if my calculation column is out of range multiple times on the same day, it will only be counted once on that day.

 

Best Regards,

Zhengdong Xu

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

6 REPLIES 6
Anonymous
Not applicable

Hi @Magnusnilsson 

I'm sorry for the mistake in my previous answer, I redid it according to your needs:

I created a table using the data you provided:

vzhengdxumsft_0-1704347849450.pngvzhengdxumsft_1-1704347849452.png

Then a calculated column is created:

warn =
var Greater_ = COUNTROWS(FILTER('Table',[Date]<=EARLIER('Table'[Date])&&[Customer]=EARLIER('Table'[Customer])&&[%]>15))
var Lower_ =COUNTROWS(FILTER('Table',[Date]<=EARLIER('Table'[Date])&&[Customer]=EARLIER('Table'[Customer])&&[%]<-15))
RETURN IF([%]>15&&Greater_>=5,1,IF([%]<-15&&Lower_>=5,1))

Greater_ and Lower_ are counted for the same user who has exceeded 15 consecutive years and is below 15 before this date, respectively
The result is as follow:

vzhengdxumsft_2-1704347937341.png

The reason for the discrepancy between the final result and your result may be that if my calculation column is out of range multiple times on the same day, it will only be counted once on that day.

 

Best Regards,

Zhengdong Xu

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Anonymous
Not applicable

Hi @Magnusnilsson

 

I have understood your approximate needs.

You want to mark values that are consecutively outside a set range with a 1.

Here is the solution:

First of all, I've created a set of data to use as an example:

vzhengdxumsft_0-1703557956306.pngvzhengdxumsft_1-1703557956307.png

Then click New Column and enter the DAX expression like this:

------------------------------------------------------------------------------------

warn =

 var a = COUNTROWS(

    FILTER('Table',[Customer]=EARLIER('Table'[Customer])&&[%]<=EARLIER('Table'[%]))

    )

 var b = IF(a>=5,1)

 return b

------------------------------------------------------------------------------------

Variable a is used to compare sales with context and then sort it.

 

The results after running are as follows:

vzhengdxumsft_2-1703557965536.png

 

Best Regards,

Zhengdong Xu

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

lbendlin
Super User
Super User

yes, that is possible.

 

Please provide sample data that covers your issue or question completely, in a usable format (not as a screenshot).

Do not include sensitive information or anything not related to the issue or question.

If you are unsure how to upload data please refer to https://community.fabric.microsoft.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-...

Please show the expected outcome based on the sample data you provided.

Want faster answers? https://community.fabric.microsoft.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447...

Hi,

 

First solution didnt work like I wanted.

I attach a table that contains the data set.

 

Little more information from me.

I would like the warning to be on a upper and a lower stage... 

For instance if the value is lower than -0,15 and over 0,15 and more than 5 in a row this should trigger the warning.

 

DateCustomerOrder%Ex. Warning
2020-02-201104851-101598519-100,1 
2020-02-201104851-101598520-100,1 
2020-02-201104851-101598522-100,14 
2020-02-281104851-101598523-10-0,04 
2020-03-051136115-101590141-1000,08 
2020-03-051136115-101590141-900,08 
2020-03-111128933-101602377-100,03 
2020-03-121128933-101602377-1-0,08 
2020-04-091128933-101602379-10-0,04 
2020-04-131136115-101590141-11-0,07 
2020-04-131136115-101590141-160-0,07 
2020-04-141128933-101602379-1-0,07 
2020-04-241104851-101602002-100,06 
2020-04-241104851-101602007-100,04 
2020-05-221128933-101602380-1-0,04 
2020-05-251128933-101602380-10-0,03 
2020-06-141136115-101590141-170-0,09 
2020-06-141136115-101590141-200-0,02 
2020-07-061128933-101615277-100,17 
2020-07-081104851-101602014-100,02 
2020-07-091104851-101602008-100,16 
2020-07-101104851-101602012-100,16 
2020-08-071128933-101615279-100,16 
2020-08-071128933-101615280-100,16 
2020-08-201128933-101615283-100,18 
2020-08-211128933-101615281-100,181
2020-08-211128933-101615285-100,4 
2020-08-241136115-101590141-21-0,16 
2020-08-241136115-101590141-240-0,2 
2020-08-261136115-101590141-220-0,19 
2020-10-151104851-101602023-100,19 
2020-10-161104851-101602017-100,19 
2020-11-031104851-101602019-100,331
2020-11-221136115-101627332-10-0,17 
2020-11-221136115-101627332-40-0,21

What is the target?

Can you guarantee that there will be maximum only one row per customer per day?

 

lbendlin_0-1704207455519.png

 

Hi,

 

Target is zero (0).

And no there can be more than one row per customer per day... 

But if it helps we can use the average of each day per customer to get one row per customer.

 

/Magnus

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

Find out what's new and trending in the Fabric community.

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.