Forum Discussion

ACraig08's avatar
ACraig08
Helper III
1 year ago
Solved

Column Dependencies/Usage Within Applied Steps

Hello, I am not even sure if this is going to make sense or not but I will do my best to explain it. I have a table that pulls in data from an Oracle database. I have applied a bunch of steps to it c...
  • ACraig08's avatar
    1 year ago

    It seems silly to reply to my own post but I thought other people might find the solution I found interesting. I was able to do it using Python coding by extracting the information from the M code using regex! 

     

    # Import required packages
    import re
    import pandas as pd
    
    # Create the variable that contains your M code
    m_code = '''
    
        Insert your M code here
    
    '''
    
    # Regular expression to find all Table.AddColumn occurrences
    add_column_pattern = r'Table\.AddColumn\([^)]+, "([^"]+)", each ([^)]+)\)'
    matches = re.findall(add_column_pattern, m_code)
    
    # Collect extracted dependencies
    data = []
    for column_name, column_expression in matches:
        # Find all references inside square brackets []
        dependency_pattern = r'\[(.*?)\]'
        
        dependencies = re.findall(dependency_pattern, column_expression)
        
        # Append results to data list
        data.append({'Column_Name': column_name, 'Dependent_On': ', '.join(set(dependencies))})
    
    # Convert to Pandas DataFrame
    df = pd.DataFrame(data)
    
    # Display extracted dependency table
    print("Extracted Dependencies Table:")
    print(df.to_string(index=False))
    
    # Export to Excel
    output_file = "column_dependencies.xlsx"
    df.to_excel(output_file, index=False)
    
    print(f"Extracted Dependencies Table saved to {output_file}")