Forum Discussion

goncalogeraldes's avatar
goncalogeraldes
Icon for Super User rankSuper User
5 years ago
Solved

Use Count as a Condition in "Custom Column" with Power Query

Hi there, I am trying to create a custom column that if the type of the transaction is of type "Financiamento", it divides the value by the count of "Financiamentos". I have tried to use DAX formulas...
  • sean_w's avatar
    sean_w
    5 years ago

    Got it.  See if this works:

     

     

    let
       Source = Table.Combine({MovimentosCBLPocas, MovimentosCBLSVTV}),
       MergeFinanciamentos = Table.NestedJoin(Source, {"Conta+Empresa"}, Financiamentos, 
       {"Conta+Empresa"}, "Financiamentos", JoinKind.LeftOuter),
       ExpandFinanciamentos = Table.ExpandTableColumn(MergeFinanciamentos, "Financiamentos", 
       {"Contratado", "Tipo"}, {"Financiamentos.Contratado", "Financiamentos.Tipo"}),
       Grouped = Table.Group(ExpandFinanciamentos, {"Conta+Empresa", "Financiamentos.Tipo"}, {{"Count", each Table.RowCount(_), Int64.Type}}),
       Filter = Table.SelectRows(Grouped, each ([Financiamentos.Tipo] = "Financiamentos")),
       MergeBack = Table.NestedJoin(ExpandFinanciamentos, {"Conta+Empresa", "Financiamentos.Tipo"}, Filter, {"Conta+Empresa", "Financiamentos.Tipo"}, "Counts", JoinKind.LeftOuter),
       ExpandTotals = Table.ExpandTableColumn(MergeBack, "Counts", {"Count"}, {"Count"}),
       Divide = Table.AddColumn(ExpandTotals, "Eval", each [Count] / [TotalUsed], Int64.Type),
       ReplaceInvalids = Table.ReplaceValue(Divide, each null, each [SingleValue],Replacer.ReplaceValue,{"Eval"})
    
    in 
       ReplaceInvalids

     

     

    Without seeing sample data and sample expected end result and going based solely off your initial code block, this is the best I've got.  It's close to what you had in your original post.  I think you were on the right track (assuming this solves your need).

     

    *Fixed an oversight where I didn't do the division*