def close_tables(egg_string, target_level=0):
    """ The egg string is hierarchically ordered. This function appends curly brackets to close open tables.
    It takes the indentation of the last closed bracket as reference for the current level/depth of the hierarchy.
    :param egg_string:
    :type egg_string: str
    :param target_level: The level determinates the target indentation/depth,
    in other words how many tables should be closed.
    :type target_level: int
    :return: egg_string with closed tables.
    :rtype: str
    """
    last_level = egg_string[egg_string[:-1].rfind('\n'):].count('  ')
    diff = last_level - target_level
    for level in range(diff):
        egg_string += '{0}}}\n'.format('  ' * (last_level - level - 1))
    return egg_string
