Forum Discussion
victorbrvs
4 years agoNew Member
How to create a column that repeat the previous value until a condition is fulfilled?
I've extracted a table from a PDF file and it looks like this
| Beach | Quality |
| Mataraca | null |
| Barra do Guajú | Good |
| Pavuna | Good |
| Baleia | Good |
| Barra do Camaratuba | Good |
| Baia da Traição | null |
| Camaratuba | Good |
| Tambá | Good |
| Praia do Forte | Good |
| Baia da Traição | Good |
| Trincheiras | Good |
| Acajutibiró | Good |
| Rio Tinto | null |
| Barra do Mamanguape | Good |
| Praia de Campina | Good |
| Praia de Oiteiro | Good |
The bold values are actually city names from which the cells below them (the actual beaches) belong, and I need to make them into another column that repeats the last city's name until it encounters a null value in "Quality", then it keeps repeating the next one. The result should look like this:
| City | Beach | Quality |
| Mataraca | Barra do Guajú | Good |
| Mataraca | Pavuna | Good |
| Mataraca | Baleia | Good |
| Mataraca | Barra do Camaratuba | Good |
| Baia da Traição | Camaratuba | Good |
| Baia da Traição | Tambá | Good |
| Baia da Traição | Praia do Forte | Good |
| Baia da Traição | Baia da Traição | Good |
| Baia da Traição | Trincheiras | Good |
| Baia da Traição | Acajutibiró | Good |
| Rio Tinto | Barra do Mamanguape | Good |
| Rio Tinto | Praia de Campina | Good |
| Rio Tinto | Praia de Oiteiro | Good |
I've tried if statements, but I don't know how to make the cell repeat itself under a condition. Any idea?
Hi,
This M code works
let Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content], #"Trimmed Text" = Table.TransformColumns(Source,{{"Beach", Text.Trim, type text}}), #"Added Custom" = Table.AddColumn(#"Trimmed Text", "Custom", each if [Quality]="null" then [Beach] else null), #"Filled Down" = Table.FillDown(#"Added Custom",{"Custom"}), #"Filtered Rows" = Table.SelectRows(#"Filled Down", each ([Quality] = "Good")), #"Reordered Columns" = Table.ReorderColumns(#"Filtered Rows",{"Custom", "Beach", "Quality"}), #"Renamed Columns" = Table.RenameColumns(#"Reordered Columns",{{"Custom", "City"}}) in #"Renamed Columns"Hope this helps.
3 Replies
- Ashish_MathurSuper User
Hi,
This M code works
let Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content], #"Trimmed Text" = Table.TransformColumns(Source,{{"Beach", Text.Trim, type text}}), #"Added Custom" = Table.AddColumn(#"Trimmed Text", "Custom", each if [Quality]="null" then [Beach] else null), #"Filled Down" = Table.FillDown(#"Added Custom",{"Custom"}), #"Filtered Rows" = Table.SelectRows(#"Filled Down", each ([Quality] = "Good")), #"Reordered Columns" = Table.ReorderColumns(#"Filtered Rows",{"Custom", "Beach", "Quality"}), #"Renamed Columns" = Table.RenameColumns(#"Reordered Columns",{{"Custom", "City"}}) in #"Renamed Columns"Hope this helps.
- victorbrvsNew Member
It worked perfectly, thank you very much Ashish!
- Ashish_MathurSuper User
You are welcome.