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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
kittipunK
Frequent Visitor

How can I add a new column that compares the status of a product in the current month to the previou

"Hi there,

I have an issue with Power Query, and I would like to create a new column called "status" to check my products based on a condition. If a product appears in the previous month, it should be labeled as "Active." If it doesn't appear, it should be labeled as "New." I've tried several formulas, but I keep getting an error code.

The error message is: 'Expression.Error: A cyclic reference was encountered during the evaluation.' Can anyone help me solve this issue?

Please let me know if you need any further information or clarification."

 

kittipunK_0-1697788529686.png

 

Thanks in advance

 

1 ACCEPTED SOLUTION

@kittipunK I did not consider this scenario (more than 1 product per month). This is even better - I simplified my code. So open Advanced Editor and replace code with the following: 

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"DATE", type date}, {"PRODUCT", type text}}),
    f = (tbl as table) =>
        [months = List.Buffer(List.Distinct(List.Transform(tbl[DATE], (x) => Date.StartOfMonth(x)))),
        status = Table.AddColumn(
            tbl, "STATUS", 
            (x) => if List.Contains(months, Date.AddMonths(Date.StartOfMonth(x[DATE]), -1)) then "Active" else "New")][status],
    group = Table.Group(#"Changed Type", "PRODUCT", {{"all", f}}),
    expand = Table.ExpandTableColumn(group, "all", {"DATE", "STATUS"})
in
    expand

View solution in original post

9 REPLIES 9
AlienSx
Super User
Super User

Hello, @kittipunK 

let
    Source = your_table,
    f = (lst as list) =>
        [maxx = List.Count(lst),
        out = List.Generate(
            () => [i = 0, r = lst{0} & [status = "New"]],
            (x) => x[i] < maxx,
            (x) => 
                [i = x[i] + 1, 
                r = lst{i} & [status = if Date.AddMonths(Date.StartOfMonth(lst{i}[DATE]), -1) = Date.StartOfMonth(lst{i - 1}[DATE]) then "Active" else "New"]],
                (x) => x[r]
        )][out],
    group = Table.Group(Source, "PRODUCT", {{"all", each f(Table.ToRecords(Table.Sort(_, "DATE")))}})[all],
    z = Table.Sort(Table.FromRecords(List.Combine(group)), "DATE")
in
    z

Thank you sir, but I try use this formula still cyclic error. I m not sure how to solved it.


@kittipunK wrote:

I m not sure how to solved it.


show your code, please. 

kittipunK_0-1698059966976.png

 

@kittipunK you're not suppose to add new column. The code is taking care of that. Now you are trying to add new column inside Table1 table with a reference to itself. This explains cyclic reference error. Please check attached file.  

Thank you, but I have an issue with the status. If I record data multiple times within a month, why is the status within the month not the same value, as shown in the picture?

 

kittipunK_0-1698072376516.png

 

@kittipunK I did not consider this scenario (more than 1 product per month). This is even better - I simplified my code. So open Advanced Editor and replace code with the following: 

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"DATE", type date}, {"PRODUCT", type text}}),
    f = (tbl as table) =>
        [months = List.Buffer(List.Distinct(List.Transform(tbl[DATE], (x) => Date.StartOfMonth(x)))),
        status = Table.AddColumn(
            tbl, "STATUS", 
            (x) => if List.Contains(months, Date.AddMonths(Date.StartOfMonth(x[DATE]), -1)) then "Active" else "New")][status],
    group = Table.Group(#"Changed Type", "PRODUCT", {{"all", f}}),
    expand = Table.ExpandTableColumn(group, "all", {"DATE", "STATUS"})
in
    expand

"Dear AlienSx,

I have a question: Is it possible for me to add a column, Status2, to check the product of the current month versus the next month? If found, show 'Active'; if not, show 'Inactive.'

Thank you."

Thank you a lot, sir. 

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors
Top Kudoed Authors