More Efficient Way to Replace Values

borkybork

New Member
Joined
Aug 5, 2022
Messages
19
Office Version
  1. 365
Platform
  1. Windows
Hi,

Is there a more efficient way to replace "Cash" with "Other Income"? I created a Group By Journal ID to identify which Journal IDs have an "Other Income" row and replaced GL Name with GroupBy.JournalID if GroupBy.JournalID is not null.

Journal IDMemoGL NameDebit/(Credit)GroupBy.JournalID
123IncomeCash100Other Income
123IncomeOther Income(100)Other Income
124IncomeCash200Other Income
124IncomeOther Income(200)Other Income
125AccrualsExpense100null
125AccrualsExpense-Tax200null
125AccrualsExpense-Rent100null
125AccrualsExpense-Bookkeeping350null
125AccrualsExpense-Supplies100null
125AccrualsLiability-Professional fees(350)null
125AccrualsLiability-Other(500)null

GroupBy Journal ID - Grouped by Journal ID and filtered for "Other Income". I then merged this query with the main one and added "GroupBy.JournalID" column .
Journal IDGL Name
123Other Income
124Other Income

I also need to replace the journal ID if it's a 1:1 entry in journals with multiple rows and add a suffix at the end, ie Journal ID 125-1.

This was my long process: I created 3 columns debit (positive values), credit (negative values) and absolute values of Debit/(Credit). Then Grouped by, merged that with the main report and added GroupBy.Credit column, then added 1:1 entry column to check if there's a match (Absolute Values = GroupBy.Credit). Then added yet another column New Journal ID to add the suffix if True.

Is there a way to achieve this with less queries and less steps? I think it's slowing down my file a lot, and it takes longer to refresh my report, sometimes it crashes if it took too long :( Any help would be greatly appreciated! Thanks so much in advance!!

Journal IDMemoGL NameDebit/(Credit)Absolute Values DebitCreditGroupBy.Credit1:1 entryNew Journal ID
125AccrualsExpense1001001000nullFalse125
125AccrualsExpense-Tax2002002000nullFalse125
125AccrualsExpense-Rent1001001000nullFalse125
125AccrualsExpense-Bookkeeping3503503500350True125-1
125AccrualsExpense-Supplies1001001000nullFalse125
125AccrualsLiability-Professional fees(350)3500350350True125-1
125AccrualsLiability-Other(500)5000500nullFalse125

Group By Journal ID, Memo and Credit, then filtered out the zeros
Journal IDMemoCredit
125Accruals350
125Accruals500
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Something like this,
Book1
ABCD
1Journal IDMemoGL NameDebit/(Credit)
2123IncomeOther Income100
3123-CRIncomeOther Income-100
4124IncomeOther Income200
5124-CRIncomeOther Income-200
6125AccrualsExpense100
7125AccrualsExpense-Tax200
8125AccrualsExpense-Rent100
9125AccrualsExpense-Bookkeeping350
10125AccrualsExpense-Supplies100
11125-CRAccrualsLiability-Professional fees-350
12125-CRAccrualsLiability-Other-500
13
14123-CRIncomeOther Income-100
15124-CRIncomeOther Income-200
16125-CRAccrualsLiability-Professional fees-350
17125-CRAccrualsLiability-Other-500
Sheet1

VBA Code:
Sub ModifyJournalEntries()
    Dim ws As Worksheet, lastRow As Long, i As Long, newRow As Long
    Set ws = ThisWorkbook.ActiveSheet
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    newRow = lastRow + 2
    
    For i = 2 To lastRow
            If ws.Cells(i, 4).Value < 0 Then
            ws.Cells(i, 1).Value = ws.Cells(i, 1).Value & "-CR"
            ws.Rows(i).Copy
            ws.Rows(newRow).PasteSpecial xlPasteValues
            newRow = newRow + 1
        End If
        
        ws.Cells(i, 3).Value = IIf(ws.Cells(i, 3).Value = "Cash", "Other Income", ws.Cells(i, 3).Value)
    Next i
    
    Application.CutCopyMode = False
    MsgBox "Journal entries modified successfully!", vbInformation
End Sub
 
Upvote 0
Something like this,
Book1
ABCD
1Journal IDMemoGL NameDebit/(Credit)
2123IncomeOther Income100
3123-CRIncomeOther Income-100
4124IncomeOther Income200
5124-CRIncomeOther Income-200
6125AccrualsExpense100
7125AccrualsExpense-Tax200
8125AccrualsExpense-Rent100
9125AccrualsExpense-Bookkeeping350
10125AccrualsExpense-Supplies100
11125-CRAccrualsLiability-Professional fees-350
12125-CRAccrualsLiability-Other-500
13
14123-CRIncomeOther Income-100
15124-CRIncomeOther Income-200
16125-CRAccrualsLiability-Professional fees-350
17125-CRAccrualsLiability-Other-500
Sheet1

VBA Code:
Sub ModifyJournalEntries()
    Dim ws As Worksheet, lastRow As Long, i As Long, newRow As Long
    Set ws = ThisWorkbook.ActiveSheet
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    newRow = lastRow + 2
   
    For i = 2 To lastRow
            If ws.Cells(i, 4).Value < 0 Then
            ws.Cells(i, 1).Value = ws.Cells(i, 1).Value & "-CR"
            ws.Rows(i).Copy
            ws.Rows(newRow).PasteSpecial xlPasteValues
            newRow = newRow + 1
        End If
       
        ws.Cells(i, 3).Value = IIf(ws.Cells(i, 3).Value = "Cash", "Other Income", ws.Cells(i, 3).Value)
    Next i
   
    Application.CutCopyMode = False
    MsgBox "Journal entries modified successfully!", vbInformation
End Sub
I think the question was how to accomplish this with Power Query, not VBA.
 
Upvote 0
Not sure I entirely get it, but this is my first attempt. Going with the provided sample, I might have over simplified.
Journal IDMemoGL NameDebit/(Credit)
123IncomeCash100
123IncomeOther Income-100
124IncomeCash200
124IncomeOther Income-200
125AccrualsExpense100
125AccrualsExpense-Tax200
125AccrualsExpense-Rent100
125AccrualsExpense-Bookkeeping350
125AccrualsExpense-Supplies100
125AccrualsLiability-Professional fees-350
125AccrualsLiability-Other-500


Why the replacement of "cash" is needed, with the required result is a mystery to me, but I left it in.
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="sample"]}[Content],
    Auto_Set_Types = Table.TransformColumnTypes(Source,{{"Journal ID", type text}, {"Memo", type text}, {"GL Name", type text}, {"Debit/(Credit)", Int64.Type}}),
    Replace_Cash = Table.ReplaceValue(Auto_Set_Types,"Cash","Other Income",Replacer.ReplaceText,{"GL Name"}),
    Temp_d_c = Table.AddColumn(Replace_Cash, "Custom", each if [#"Debit/(Credit)"] < 0 then "credit" else "debit"),
    Pivot_d_c = Table.Pivot(Temp_d_c, List.Distinct(Temp_d_c[Custom]), "Custom", "Debit/(Credit)"),
    c_as_abs_val = Table.TransformColumns(Pivot_d_c,{{"credit", Number.Abs, Int64.Type}}),
    New_id = Table.ReplaceValue(c_as_abs_val,each [Journal ID],each if [debit]=null then [Journal ID]& "-1" else [Journal ID],Replacer.ReplaceValue,{"Journal ID"}),
    Filter_on_id = Table.SelectRows(New_id, each ([Journal ID] = "125-1")),
    Keep_cols = Table.RemoveColumns(Filter_on_id,{"GL Name", "debit"})
in
    Keep_cols
 
Upvote 0

Forum statistics

Threads
1,226,506
Messages
6,191,439
Members
453,658
Latest member
healmo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top