Forum Discussion

romovaro's avatar
romovaro
Icon for Responsive Resident rankResponsive Resident
2 years ago
Solved

If Customer with Same Country and Same GLD = Same Project

HI,

 

Happy Friday. I need help with one formula I have in Excel.

=IF(A2<>A3,1,IF(AND(A2=A3,D2<>D3),1,IF(AND(A2=A3,D2=D3,E2=E3),0,1)))

 

Where A is CID Number

Where D is Country

Where E is GLD

 

The idea is to count the number of projects. If a Customer (CID) have CUIDs with the same country and Same GLD (Go live Date)

From the table below, the ones highlighted in red are considered 1 project.

 

CIDClient NameId CountryCUIDCountryGLDNum Projects
10002Customer AUSCustA1United States9/1/20220
10002Customer AUSCustA2United States9/1/20221
10022Customer BINCustB1India12/1/20211
10022Customer BSECustB2Sweden4/1/20241
10027Customer CAUCustC1Australia5/1/20221
10038Customer DEGCustD1Egypt3/1/20241
10047Customer ESGCustE1Singapore7/1/20221
10065Customer FSGCustF1Singapore6/1/20221
10109Customer GATCustG1Austria8/1/20221
10109Customer GBRCustG2Brazil6/1/20230
10109Customer GBRCustG3Brazil6/1/20230
10109Customer GBRCustG4Brazil6/1/20231
10109Customer GCRCustG5Costa Rica7/1/20221
10109Customer GDKCustG6Denmark5/1/20231
10109Customer GEECustG7Estonia11/1/20211
10109Customer GEECustG8Estonia2/1/20241
10109Customer GDECustG9Germany10/1/20211
10109Customer GGRCustG10Greece10/1/20211
10109Customer GKRCustG11Korea, Republic Of2/1/20221
10109Customer GLVCustG12Latvia10/1/20210
10109Customer GLVCustG13Latvia10/1/20211
10109Customer GLTCustG14Lithuania10/1/20211
10109Customer GLUCustG15Luxembourg1/1/20241
10109Customer GMXCustG16Mexico2/1/20220
10109Customer GMXCustG17Mexico2/1/20221
10109Customer GNOCustG18Norway2/1/20241
10109Customer GPACustG19Panama7/1/20221
10109Customer GRSCustG20Serbia2/1/20241
10109Customer GSGCustG21Singapore2/1/20221

 

for the Excel formula to work, I have the Excel sorted to get CID/Country and GLD (from Oldest to Newest). and the formula filters if Same CID, Same COuntry (from previous row) And Same GLD (from previous Row)

= 0 and if not = 1. Then I can count the number of projects.

 

Trying to get the same in PowerBI but not with good results. Any tip is welcome

 

thanks,

 

 

  • It seems I forgot an "OR" for the final filter.

     

    NumProjects = IF (
        OR ( OR (
            'Weekly Slippage'[CID]<>
        CALCULATE (
            MAX ('Weekly Slippage'[CID]),
            FILTER (
                'Weekly Slippage',
                'Weekly Slippage'[Index] = EARLIER ('Weekly Slippage'[Index])-1)
        ),
        'Weekly Slippage'[Country]<>
        CALCULATE (
            MAX ('Weekly Slippage'[Country]),
            FILTER (
                'Weekly Slippage',
                'Weekly Slippage'[Index] = EARLIER ('Weekly Slippage'[Index])-1)
        )
        ),
            'Weekly Slippage'[Ops Forecast & Actuals]<>
            CALCULATE(
                MAX('Weekly Slippage'[Ops Forecast & Actuals]),
                FILTER(
                    'Weekly Slippage',
                    'Weekly Slippage'[Index] = EARLIER('Weekly Slippage'[Index]) -1)
            )
        )
        ,1
        ,0
        )
  • jgeddes's avatar
    jgeddes
    2 years ago

    If you want the rows with '1' to display the sum of 'Contract EE' and the coresponding range then change the value in the _size variable.

    FROM

    var _size =
    IF(
        [Num Projects] = 1,
        [Contract EE],
        CALCULATE(SUM('Table'[Contract EE]),ALLEXCEPT('Table', 'Table'[Id Country], 'Table'[GLD]))
    )

     TO

    var _size =
    IF(
        [Num Projects] = 0,
        [Contract EE],
        CALCULATE(SUM('Table'[Contract EE]),ALLEXCEPT('Table', 'Table'[Id Country], 'Table'[GLD]))
    )

16 Replies

  • You need to sort by CID, Country, and GLD then create a calculated column that replicates the logic of your Excel formula :

    Num Projects =
    VAR CurrentRow = TableName
    VAR PreviousRow =
    CALCULATETABLE(
    TableName,
    TableName[Index] = CurrentRow[Index] - 1
    )
    RETURN
    IF (
    CurrentRow[CID] <> PreviousRow[CID],
    1,
    IF (
    AND(
    CurrentRow[CID] = PreviousRow[CID],
    CurrentRow[Country] <> PreviousRow[Country]
    ),
    1,
    IF (
    AND(
    CurrentRow[CID] = PreviousRow[CID],
    CurrentRow[Country] = PreviousRow[Country],
    CurrentRow[GLD] = PreviousRow[GLD]
    ),
    0,
    1
    )
    )
    )
    • romovaro's avatar
      romovaro
      Icon for Responsive Resident rankResponsive Resident

      Thanks Amira

       

      I created a custom Sort in Power Query: CID/Country and GLD

       

       

      and Index Column

       

      but when creating the calculated column, I have some issues with the variables Current row/Previous Row

      Am I making a mistake?

      I also have the error: Too many arguments have been passed to the AND function. The maximum number of arguments for the function is 2.

       

       

       

      Thanks,

       

       

       

       

       

      g

       

       

    • romovaro's avatar
      romovaro
      Icon for Responsive Resident rankResponsive Resident

      Hi AmiraBedh 

       

      I found a different solution but not working for the 3 filters I want to apply. 

       

      After Sorting and creating an Index formula.

       

      IF same Customer 1 or 0 it works:

       

      Project = IF('Weekly Slippage'[CID]<>
          CALCULATE(
              MAX ('Weekly Slippage'[CID]),
              FILTER ('Weekly Slippage',
              'Weekly Slippage'[Index]=EARLIER('Weekly Slippage'[Index])-1
              )
          ),
          1,0)
       
       
      When same customer and COuntry 1 or 0, it works:
      Project2 = IF(OR('Weekly Slippage'[CID]<>
          CALCULATE (
              MAX ('Weekly Slippage'[CID]),
              FILTER ('Weekly Slippage',
              'Weekly Slippage'[Index] = EARLIER ('Weekly Slippage'[Index])-1)
          ),
              'Weekly Slippage'[Country]<>
              CALCULATE(
                  MAX('Weekly Slippage'[Country]),
                  FILTER(
                      'Weekly Slippage',
                      'Weekly Slippage'[Index] = EARLIER('Weekly Slippage'[Index]) -1)
              )
      ),1,0)
       
      But when I try the 3 filters. Same Customer/Country and Date...I have an error but I don't know why:
      NumProjects = IF (
          OR(
              'Weekly Slippage'[CID]<>
          CALCULATE (
              MAX ('Weekly Slippage'[CID]),
              FILTER (
                  'Weekly Slippage',
                  'Weekly Slippage'[Index] = EARLIER ('Weekly Slippage'[Index])-1)
          ),
          'Weekly Slippage'[Country]<>
          CALCULATE (
              MAX ('Weekly Slippage'[Country]),
              FILTER (
                  'Weekly Slippage',
                  'Weekly Slippage'[Index] = EARLIER ('Weekly Slippage'[Index])-1)
          )
          ),
              'Weekly Slippage'[Ops Forecast & Actuals]<>
              CALCULATE(
                  MAX('Weekly Slippage'[Ops Forecast & Actuals]),
                  FILTER(
                      'Weekly Slippage',
                      'Weekly Slippage'[Index] = EARLIER('Weekly Slippage'[Index]) -1)
              )
          ),1,0)
       
      It seems the error is at the end but....
         

       

       ANy tip is welcome
       
       Thanks
      • jgeddes's avatar
        jgeddes
        Icon for Super User rankSuper User

        Try this code in Power Query. (Paste into the advanced editor of a blank query)

        let
            Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("pZXbjoIwEIZfhXhtIhSPl6hIjMfIutnEeFG165KVYiqsuk+/HYVSFCtmr+xM+/0zHX5wsSgZuq6jUrnUiQ5h4BOmWTyYu3HGMiCiXkg2mhvikBx43KoYFaQjoPTSsvxUA6k1jEQDZTTaPOiP40wb+ujTjYcBQFfYUMOuncCw5R7JhlC+qF7hagZuyHCHB9Y8znSgisUXDO8u1Ws5nZtNme/ywHbiTBd4e3veh/zXzKldzdSGnt2EtYF1PbrF+4ARvm7k1K7XZL4n871bvn7PG3pL5gG13uKMI+5+uXmzEN2eJTQcbDP86+3S0qZkGiVs/geuPoAVbXcEfJlncAixNvPWOH/qOQLdQSJQh4hQH7Pv1C9PytuJWR2wg8136NXrxr3XlXgzg6N7w+W1LmjYcgjzMT0Dohcq7ojRGTqEjJA1KY4PUhxODrhVcVmbkX202nlrbfKZ3uPJIxi+CyU4OcThz3WIUiMK/0i4+QhXVU9fG5j30Au/IkxfUpgLBTDhMDoRfxVEbAtYoSc5+hAK4MIROXnrIDs/xQAkuvGAVtQeTwQNJhwH7IjPhT04tQQNe1NMsV/85ZslfzYOAgu6hK1e8L/4YDro9ouZvfryDw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [CID = _t, #"Client Name" = _t, #"Id Country" = _t, CUID = _t, Country = _t, GLD = _t, #"Num Projects" = _t]),
            #"Changed Type" = Table.TransformColumnTypes(Source,{{"CID", Int64.Type}, {"Client Name", type text}, {"Id Country", type text}, {"CUID", type text}, {"Country", type text}, {"GLD", type date}, {"Num Projects", Int64.Type}}),
            #"Removed Columns" = Table.RemoveColumns(#"Changed Type",{"Num Projects"}),
            #"Added Index" = Table.AddIndexColumn(#"Removed Columns", "Index", 1, 1, Int64.Type),
            #"Grouped Rows" = Table.Group(#"Added Index", {"CID", "Client Name", "Id Country", "Country"}, {{"_nestedTable", each _, type table [CID=nullable number, Client Name=nullable text, Id Country=nullable text, CUID=nullable text, Country=nullable text, GLD=nullable date]}}),
            Custom1 = Table.TransformColumns(#"Grouped Rows", {{"_nestedTable", each Table.AddColumn(_, "NumProjects", (x)=> if x[Index] = List.Max([Index]) then 1 else 0)}}),
            #"Expanded _nestedTable" = Table.ExpandTableColumn(Custom1, "_nestedTable", {"CUID", "GLD", "NumProjects"}, {"CUID", "GLD", "NumProjects"})
        in
            #"Expanded _nestedTable"
  • romovaro's avatar
    romovaro
    Icon for Responsive Resident rankResponsive Resident

    Hi Amira

     

    Any idea why my var is not being recognized?

     

    thanks,