Forum Discussion
Need halp in solving copying row value in table
Hi.
I need help in write DAX to create column base on the condition for example . If columnA="500" and columnB="S" then copy value from abov row as shown in Result (new col) other wise leave it blank.
With thanks !
Mohsin
Different approach using offset. Looks at previous row in data when sorted by date and pulls from it when current row is 900 and fever.
Project Leader Filled = VAR _prevRow = OFFSET( -1, ALL( Data[date], Data[Projectnummer], Data[Articlanaame] ), ORDERBY( Data[date], ASC ) ) VAR _prevProjArticle = SUMMARIZE( _prevRow, Data[Projectnummer], Data[Articlanaame] ) VAR _curIsNewProjArticle = NOT ( Data[Projectnummer], Data[Articlanaame] ) IN _prevProjArticle RETURN IF( Data[Projectnummer] = "900" && Data[Articlanaame] = "fever", IF( _curIsNewProjArticle, CALCULATE( VALUES( Data[projectleader] ), REMOVEFILTERS( Data ), _prevRow ), Data[projectleader] ), Data[projectleader] )Category Filled = VAR _prevRow = OFFSET( -1, ALL( Data[date], Data[Projectnummer], Data[Articlanaame] ), ORDERBY( Data[date], ASC ) ) VAR _prevProjArticle = SUMMARIZE( _prevRow, Data[Projectnummer], Data[Articlanaame] ) VAR _curIsNewProjArticle = NOT ( Data[Projectnummer], Data[Articlanaame] ) IN _prevProjArticle RETURN IF( Data[Projectnummer] = "900" && Data[Articlanaame] = "fever", IF( _curIsNewProjArticle, CALCULATE( VALUES( Data[Projectcategory] ), REMOVEFILTERS( Data ), _prevRow ), Data[Projectcategory] ), Data[Projectcategory] )I stacked on a duplicate of previous sample with rolled forward dates to mimic actual data better (multiple groups of 900,fever)
Data
date Name Projectnummer Articlanaame projectleader Projectcategory 1/3/2022 Martin 900 paid holiday 1/4/2022 Martin 900 paid holiday 1/5/2022 Martin 900 paid holiday 1/7/2022 Martin 900 paid holiday 1/10/2022 Martin 201690 work time patrik construction 1/11/2022 Martin 201690 work time patrik construction 1/12/2022 Martin 201690 temperory work patrik construction 1/13/2022 Martin 201690 temperory work patrik construction 1/14/2022 Martin 201690 temperory work patrik construction 1/17/2022 Martin 201690 temperory work patrik construction 1/18/2022 Martin 900 fever 1/19/2022 Martin 900 fever 1/20/2022 Martin 900 fever 1/21/2022 Martin 900 fever 1/24/2022 Martin 900 fever 1/25/2022 Martin 900 fever 1/26/2022 Martin 900 paid holiday 1/27/2022 Martin 900 paid holiday 1/28/2022 Martin 900 paid holiday 1/30/2022 Martin 900 paid holiday 2/2/2022 Martin 201690 work time patrik construction 2/3/2022 Martin 201690 work time patrik construction 2/4/2022 Martin 201690 temperory work patrik construction 2/5/2022 Martin 201690 temperory work patrik construction 2/6/2022 Martin 201690 temperory work patrik construction 2/9/2022 Martin 201690 temperory work patrik construction 2/10/2022 Martin 900 fever 2/11/2022 Martin 900 fever 2/12/2022 Martin 900 fever 2/13/2022 Martin 900 fever 2/16/2022 Martin 900 fever 2/17/2022 Martin 900 fever Result with new columns:
19 Replies
- ChielFaberSuper User
The easiest way will probably be to use the new (preview) visual calculations.
check out: https://learn.microsoft.com/en-us/power-bi/transform-model/desktop-visual-calculations-overview
And especially the previous function from visual calculations: https://learn.microsoft.com/en-us/dax/previous-function-dax
Hope this is helpfull
Regards,
Chiel
- mohsin-razaHelper III
I try to clear the problem with more better way
I want to copy from pervious date value project leader and task based on the condition if project="900" and artical name ="fever" then copy pervious project leader value and task .
- ChielFaberSuper User
Visual calculations is the way to go.
I recreated your sample data.
For this to work you need to enable visual calculations in the options menu (preview options).
Put the fields in a matrix. press the three dots on the right upper corner and select new visual calculation: custom
You can then add in a new column calculation:
project leader
Project leader (filled) =IF ([Project] = "900"&& [Articalname] = "fever",COALESCE ( PREVIOUS([project leader]), [project leader] ),[project leader])TaskTask (filled) =IF ([Project] = "900"&& [Articalname] = "fever",COALESCE ( PREVIOUS([task]), [task] ),[task])This will give you the desired outcome:To me this feels the easiest way to accomplish your goal.
Hope this is helpfull.
Regards,
Chiel
- mohsin-razaHelper III
Thanks a lot for your kind answer . When I try to implement as an add column, I get this error
How to solve this problem?
regards
- MarkLafSuper User
Here is a solution that I think is following your requirements.
Here are test data I used, pulled from your snips but with an extra project to show how we resolve for when multiple projects fall on the latest previous date. Also, note that this only works if your Date column is of Date data type (or something that sorts appropriately).
Data
Date Name Project ArticleName Project Leader Task 1/12/2022 Martin 201690 Ordinarie Andersson renovation 1/13/2022 Martin 201690 Ordinarie Andersson renovation 1/14/2022 Martin 201690 Ordinarie Andersson renovation 1/17/2022 Martin 201690 Ordinarie Andersson renovation 1/17/2022 Martin XXX YYY AAA BBB 1/18/2022 Martin 900 fever 1/19/2022 Martin 900 fever 1/20/2022 Martin 900 fever 1/21/2022 Martin 900 fever DAX for columns
Project Leader Filled = VAR _thisRank = RANK( ORDERBY( Data[Date], ASC ), PARTITIONBY( Data[Project] ) ) VAR _thisDate = Data[Date] VAR _validRows = FILTER( ALL( Data[Date], Data[Project], Data[Project Leader], Data[Task] ), Data[Project Leader] <> BLANK() && Data[Task] <> BLANK() // assuming you want non-blank but remove otherwise && Data[ArticleName] <> "900" && Data[Date] < _thisDate ) VAR _validPrevious = INDEX( 1, _validRows, ORDERBY( Data[Date], DESC, Data[Project], ASC // if this were DESC, then result would be "AAA" ) ) RETURN IF( Data[Project] = "900" && Data[ArticleName] = "fever" && _thisRank = 1, CALCULATE( VALUES( Data[Project Leader] ), _validPrevious, REMOVEFILTERS( Data ) ), Data[Project Leader] )Task Filled = VAR _thisRank = RANK( ORDERBY( Data[Date], ASC ), PARTITIONBY( Data[Project] ) ) VAR _thisDate = Data[Date] VAR _validRows = FILTER( ALL( Data[Date], Data[Project], Data[Project Leader], Data[Task] ), Data[Project Leader] <> BLANK() && Data[Task] <> BLANK() // assuming you want non-blank but remove otherwise && Data[ArticleName] <> "900" && Data[Date] < _thisDate ) VAR _validPrevious = INDEX( 1, _validRows, ORDERBY( Data[Date], DESC, Data[Project], ASC // if this were DESC, then result would be "BBB" ) ) RETURN IF( Data[Project] = "900" && Data[ArticleName] = "fever" && _thisRank = 1, CALCULATE( VALUES( Data[Task] ), _validPrevious, REMOVEFILTERS( Data ) ), Data[Task] )Output
- mohsin-razaHelper III
I am very much thank full you effort and reply. Can you see above the excel sheet I share .? I do not want the Andersson or renovation to inserted but AAA and BBB be inserted on condition "900" and "fever" and on just "january 18,2022" not on january 19 or 20 .
when I try the test your code .It is not inserting pervious date coumn value.
Can you please modify the above code?
With lot of regards.
- AnonymousNot applicable
Hi mohsin-raza ,
Thank you for reaching out to the Microsoft fabric community forum and I appreciate the helpful guidance already provided by ChielFaber .
Could you please confirm if the issue has been resolved. I wanted to check if you had the opportunity to review the information provided by ChielFaber . Please feel free to contact us if you have any further questions.
Thank you.
- mohsin-razaHelper III
Anonymous Thanks for your kind mail.
I am still struggling with same problem . I needs it in as calculated column and over 4 years of data .
regards
- AnonymousNot applicable
Hi mohsin-raza ,
I wanted to check if you had the opportunity to review the information provided. Please feel free to contact us if you have any further questions.
Thank you. - AnonymousNot applicable
Hi mohsin-raza ,
We haven’t received an update from you in some time. Could you please let us know if the issue has been resolved?
If you still require support, please let us know, we are happy to assist you.Thank you.