Forum Discussion

chdcfs's avatar
chdcfs
Regular Visitor
1 year ago
Solved

Creating a new column in a visual that is the difference between the counts of two existing columns

I have a visual that is a matrix.

 

Col 3 is a count and Col 5 is a count.

I want to create a column that is the difference between Col 3 and Col 5.

 

I can't seem to find anything on creating an additional column with a formula inside a visual.

  • Anonymous's avatar
    Anonymous
    1 year ago

    Thanks for the reply from Ritaf1983  and speedramps , please allow me to provide another insight:

    Hi, chdcfs 
    Thanks for reaching out to the Microsoft fabric community forum.

    Regarding the issue you raised, my solution is as follows:

    1.Assuming your data structure is as follows:

    2.Here is the original visualization in the matrix:

    3.You can try processing it using the following M language:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUXLOz1EwVorVoZxnioXnhKISO8+UIM8ZRR92nil+XiwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", type text}}),
        #"Grouped Rows" = Table.Group(#"Changed Type", {"Column1", "Column2"}, {{"Count", each Table.RowCount(_), Int64.Type}}),
        #"Pivoted Column" = Table.Pivot(#"Grouped Rows", List.Distinct(#"Grouped Rows"[Column2]), "Column2", "Count"),
        #"Added Custom" = Table.AddColumn(#"Pivoted Column", "DIFF", each [Col 3]-[Col 5]),
        #"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"DIFF", Int64.Type}}),
        #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type1", {"Column1"}, "Attribute", "Value")
    in
        #"Unpivoted Columns"
    

    4.Here's my final result, which I hope meets your requirements.

    Can you share sample data and sample output in tabular format if I am misunderstanding? Or a sample pbix after removing sensitive data. We can better understand the problem and help you.When uploading a file, please be careful to delete sensitive information.

     

    For questions about uploading data, you can try the following links:

    How to provide sample data in the Power BI Forum - Microsoft Fabric Community

    Solved: How to upload PBI in Community - Microsoft Fabric Community

     

    Please find the attached pbix relevant to the case.

     

    Best Regards,

    Leroy Lu

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

3 Replies

  • Try this ..

     

     

     

    U days = 
    CALCULATE(
        DISTINCTCOUNT('Calendar'[Day name]),
        CONTAINSSTRING('Calendar'[Day name] , "U")
    )
    

     

     

     

    S days = 
    CALCULATE(
        DISTINCTCOUNT('Calendar'[Day name]),
        CONTAINSSTRING('Calendar'[Day name] , "S")
    )

     

     

     

    Difference = 
    [U days] - [S days]

     

     

     

    Please click the [accept as solution] and the thumbs up button. Thank you

  • Hi speedramps 
    If I understood you correctly you are talking about visual calculation.
    Which is work as a feature that allows us to make calculations based on visual.

    Please take a look at the linked video:

    https://www.youtube.com/watch?v=FSdGyeNGss8

    if it is something else

    please provide some raw data in your tables (exclude sensitive data) with Text format and your expected result with backend logic and special examples. It would be helpful to find out the solution. You can refer the following link to share the required info:

    How to provide sample data in the Power BI Forum

    And It is better if you can share a simplified pbix file. You can refer the following link to upload the file to the community. Thank you.

    How to upload PBI in Community

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

     

  • Anonymous's avatar
    Anonymous
    Not applicable

    Thanks for the reply from Ritaf1983  and speedramps , please allow me to provide another insight:

    Hi, chdcfs 
    Thanks for reaching out to the Microsoft fabric community forum.

    Regarding the issue you raised, my solution is as follows:

    1.Assuming your data structure is as follows:

    2.Here is the original visualization in the matrix:

    3.You can try processing it using the following M language:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUXLOz1EwVorVoZxnioXnhKISO8+UIM8ZRR92nil+XiwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", type text}}),
        #"Grouped Rows" = Table.Group(#"Changed Type", {"Column1", "Column2"}, {{"Count", each Table.RowCount(_), Int64.Type}}),
        #"Pivoted Column" = Table.Pivot(#"Grouped Rows", List.Distinct(#"Grouped Rows"[Column2]), "Column2", "Count"),
        #"Added Custom" = Table.AddColumn(#"Pivoted Column", "DIFF", each [Col 3]-[Col 5]),
        #"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"DIFF", Int64.Type}}),
        #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type1", {"Column1"}, "Attribute", "Value")
    in
        #"Unpivoted Columns"
    

    4.Here's my final result, which I hope meets your requirements.

    Can you share sample data and sample output in tabular format if I am misunderstanding? Or a sample pbix after removing sensitive data. We can better understand the problem and help you.When uploading a file, please be careful to delete sensitive information.

     

    For questions about uploading data, you can try the following links:

    How to provide sample data in the Power BI Forum - Microsoft Fabric Community

    Solved: How to upload PBI in Community - Microsoft Fabric Community

     

    Please find the attached pbix relevant to the case.

     

    Best Regards,

    Leroy Lu

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